zoukankan      html  css  js  c++  java
  • iOS常用技术-序列帧动画

     1 //
     2 //  ViewController.m
     3 //  序列帧动画
     4 //
     5 //  Created by 大欢 on 16/1/20.
     6 //  Copyright © 2016年 bjsxt. All rights reserved.
     7 //
     8 
     9 #import "ViewController.h"
    10 #import "UIImageView+GIF.h"
    11 
    12 @interface ViewController ()
    13 - (IBAction)playAction:(id)sender;
    14 - (IBAction)stopAction:(id)sender;
    15 
    16 @property (nonatomic, strong) UIImageView * imageView;
    17 
    18 @end
    19 
    20 @implementation ViewController
    21 
    22 - (void)viewDidLoad {
    23     [super viewDidLoad];
    24     
    25     //播放gif动画
    26     NSString * path = [[NSBundle mainBundle] pathForResource:@"hehua" ofType:@"gif"];
    27     UIImageView * imageView = [UIImageView imageViewWithGIFFile:path frame:CGRectMake(100, 100, 200, 200)];
    28     [self.view addSubview:imageView];
    29 
    30 }
    31 
    32 - (void)animation {
    33     
    34     NSMutableArray * array = [NSMutableArray array];
    35     
    36     for (int i = 1; i < 6; i++) {
    37         
    38         NSString * string = [NSString stringWithFormat:@"hehua0%d",i];
    39         UIImage * image = [UIImage imageNamed:string];
    40         [array addObject:image];
    41     }
    42     
    43     self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
    44     //设置动画图片
    45     self.imageView.animationImages = array;
    46     //动画时间
    47     self.imageView.animationDuration = 1;
    48     //动画播放重复次数,值为0时,无限循环
    49     self.imageView.animationRepeatCount = 0;
    50     //开始动画
    51     [self.imageView startAnimating];
    52     [self.view addSubview:self.imageView];
    53 }
    54 
    55 
    56 - (IBAction)playAction:(id)sender {
    57     
    58     [self.imageView stopAnimating];
    59 }
    60 
    61 - (IBAction)stopAction:(id)sender {
    62     
    63     [self.imageView startAnimating];
    64 }
    65 @end

    /********************************************/

     1 //
     2 //  UIImageView+GIF.h
     3 //  day3
     4 //
     5 //  Created by andezhou on 16/1/2.
     6 //  Copyright © 2016年 周安德. All rights reserved.
     7 //
     8 
     9 #import <UIKit/UIKit.h>
    10 
    11 @interface UIImageView (GIF)
    12 
    13 // 从指定的路径加载GIF并创建UIImageView
    14 + (UIImageView *)imageViewWithGIFFile:(NSString *)file frame:(CGRect)frame;
    15 
    16 @end

    /************************************************/

     1 //
     2 //  UIImageView+GIF.m
     3 //  day3
     4 //
     5 //  Created by andezhou on 16/1/2.
     6 //  Copyright © 2016年 周安德. All rights reserved.
     7 //
     8 
     9 #import "UIImageView+GIF.h"
    10 #import <ImageIO/ImageIO.h>
    11 #import <CoreText/CoreText.h>
    12 /*
    13  ARC下需要手动管理内存的有(Core)
    14  CT开头,例如:CTRunRef
    15  CF开头,例如:CTFrameRef
    16  CG开头,例如:CGImageSourceRef
    17  */
    18 
    19 @implementation UIImageView (GIF)
    20 
    21 + (UIImageView *)imageViewWithGIFFile:(NSString *)file frame:(CGRect)frame
    22 {
    23     UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
    24     // 加载gif文件数据
    25     NSData *gifData = [NSData dataWithContentsOfFile:file];
    26     
    27     // GIF动画图片数组
    28     NSMutableArray *frames = nil;
    29     // 图像源引用
    30     CGImageSourceRef src = CGImageSourceCreateWithData((__bridge CFDataRef)gifData, NULL);
    31     // 动画时长
    32     CGFloat animationTime = 0.f;
    33     
    34     if (src) {
    35         // 获取gif图片的帧数
    36         size_t count = CGImageSourceGetCount(src);
    37         // 实例化图片数组
    38         frames = [NSMutableArray arrayWithCapacity:count];
    39         
    40         for (size_t i = 0; i < count; i++) {
    41             // 获取指定帧图像
    42             CGImageRef image = CGImageSourceCreateImageAtIndex(src, i, NULL);
    43             
    44             // 获取GIF动画时长
    45             NSDictionary *properties = (__bridge NSDictionary *)CGImageSourceCopyPropertiesAtIndex(src, i, NULL);
    46             NSDictionary *frameProperties = [properties objectForKey:(NSString *)kCGImagePropertyGIFDictionary];
    47             NSNumber *delayTime = [frameProperties objectForKey:(NSString *)kCGImagePropertyGIFUnclampedDelayTime];
    48             animationTime += [delayTime floatValue];
    49             
    50             if (image) {
    51                 [frames addObject:[UIImage imageWithCGImage:image]];
    52                 CGImageRelease(image);
    53             }
    54         }
    55         
    56         CFRelease(src);
    57     }
    58     
    59     [imageView setImage:[frames objectAtIndex:0]];
    60     [imageView setBackgroundColor:[UIColor clearColor]];
    61     [imageView setAnimationImages:frames];
    62     [imageView setAnimationDuration:animationTime];
    63     [imageView startAnimating];
    64     
    65     return imageView;
    66 }
    67 
    68 @end

    /*****************************************************/

  • 相关阅读:
    模糊化控制
    第8章 控制对象的访问(setter、getter、proxy)
    第7章 面向对象与原型
    Termux键盘配置
    在md里画流程图
    设置浏览器不缓存文件
    安卓手机使用Termux及搭建FTP服务器
    第6章 未来的函数:生成器和promise
    第5章 精通函数:闭包和作用域
    第4章 函数进阶:理解函数调用
  • 原文地址:https://www.cnblogs.com/MrWuYindi/p/5146700.html
Copyright © 2011-2022 走看看