iOS加载Gif动态图

1、Gif在iOS开发中的说明

  • Gif 图片是非常常见的图片格式,尤其是在聊天的过程中,Gif 表情使用地很频繁。但是 iOS 没有现成的支持加载和播放 Gif 的类。

2、加载本地Gif

  • 2.1 SDWebImage加载

    • 在 SDWebImage 这个库里有一个 UIImage+GIF 的类别,里面为 UIImage 扩展了三个方法。
    @interface UIImage (GIF)
    + (IImage *)sd_animatedGIFNamed:(NSString *)name;
    + (UIImage *)sd_animatedGIFWithData:(NSData *)data;
    - (UIImage *)sd_animatedImageByScalingAndCroppingToSize:(CGSize)size;
    @end
    
    • 我们要获取处理后的 Gif 图片,其实只要调用前面两个中的其中一个方法就行了,注意,第一个只需要传 Gif 的名字,而不需要带扩展名(如 Gif 图片名字为 demo_gif_001.gif,只需传 demo_gif_001 即可)。
    #import "UIImage+GIF.h"
    
    UIImageView *sImg = [[UIImageView alloc] init];
    [self addSubview:sImg];
    sImg.frame = CGRectMake(30, 50, 400, 300);
    
    NSString *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:@"demo_gif_001" ofType:@"gif"];
    NSData *imageData = [NSData dataWithContentsOfFile:filePath];
    sImg.image = [UIImage sd_animatedGIFWithData:imageData];
    
    • 注意:gif 图片不要放在 .xcassets 资源文件夹下,否则加载不出来。
    • 效果
  • 2.2 使用 UIWebView 加载

    • 使用 UIWebView 的弊端在于,不能设置 Gif 动画的播放时间,不能自动缩放 Gif 动图。
    // 读取 GIF 图片数据
    NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"demo3" ofType:@"gif"]];
    
    // 加载 GIF 动图
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 200)];
    [webView loadData:data MIMEType:@"image/gif" textEncodingName:@"" baseURL:[NSURL URLWithString:@""]];
    
    [self.view addSubview:webView];
    
  • 2.3 使用 QExtension 加载

// 通过名称加载 gif 图片,不需要写扩展名
self.imageView.image = [UIImage q_gifImageNamed:@"demo3"];

// 通过数据加载 gif 图片
NSData *imageData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"demo3" ofType:@"gif"]];
self.imageView.image = [UIImage q_gifImageWithData:imageData];
  • 效果

  • 2.4 将 Gif 动图拆分成多张图片,使用 UIImageView 播放

    • 最好把所需要的 Gif 图片打包到 Bundle 文件内。下拉、上拉加载控件需要一个根据拉动距离设置特定的 Image,一般使用该方法。
    - (NSArray *)animationImages {
    	NSFileManager *fielM = [NSFileManager defaultManager];
    	NSString *path = [[NSBundle mainBundle] pathForResource:@"Loading" ofType:@"bundle"];
    	NSArray *arrays = [fielM contentsOfDirectoryAtPath:path error:nil];
    
    	NSMutableArray *imagesArr = [NSMutableArray array];
    	for (NSString *name in arrays) {
    		UIImage *image = [UIImage imageNamed:[(@"Loading.bundle") stringByAppendingPathComponent:name]];
    		if (image) {
    			[imagesArr addObject:image];
    		}
    	}
    	return imagesArr;
    }
    
    - (void)viewDidLoad {
    	[super viewDidLoad];
    
    	UIImageView *gifImageView = [[UIImageView alloc] initWithFrame:frame];
    	gifImageView.animationImages = [self animationImages]; // 获取 Gif 图片列表
    	gifImageView.animationDuration = 5;                    // 执行一次完整动画所需的时长
    	gifImageView.animationRepeatCount = 1;                 // 动画重复次数
    	[gifImageView startAnimating];
    	[self.view addSubview:gifImageView];
    }
    

2、加载网络 GIF 动图

  • 2.1 使用 SDWebImage 加载

    • 加载网络的 Gif 文件就简单多了。最简单的方法,我们只需要使用 SDWebImage 的 sd_setImageWithURL: 这个方法传入 Gif 文件是 url 地址即可。
    NSURL *url = [NSURL URLWithString:@"http://images.cnblogs.com/cnblogs_com/QianChia/934664/o_QExtension31.gif"];
    [self.imageView sd_setImageWithURL:url];
    
    • SDWebImage 内部实现大概是以下几个步骤:
      • 1、SDWebImage 根据 url 将 Gif 文件下载下来,格式为一个 NSData。
      • 2、如果判断是 Gif 格式,则会调用 sd_animatedGIFWithData: 将 Data 转换成我们需要的 Gif 格式。
      • 3、通过上面的方法二即可显示出 Gif 图片。
    UIImage *image = [UIImage sd_animatedGIFWithData:data];
    gifImageView.image = image;
    
原文地址:https://www.cnblogs.com/CH520/p/13781389.html