iOS加载GIF图

作者: 壮骨 | 来源:发表于2017-08-23 15:42 被阅读213次

1.WebView

UIWebView
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,200,200)];
[self.view addSubview:webView]; 
NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"gif"];
NSURL *url = [NSURL URLWithString:path]; // 可以直接是网络URL
[webView loadRequest:[NSURLRequest requestWithURL:url]];
UIWebView实现代码截图
WKWebView

WKWebView实习和UIWebView一样,也是将GIF图片的URL加载出来

WKWebView代码实现

如果 [[NSBundle mainBundle] pathForResource:@"" ofType:@""] 无法获取到文件
将一个文件导入到工程中后,用[[NSBundle mainBundle] pathForResource:@"" ofType:@""]来获取到该文件时,一直无法拿到这个文件,解决方法如下

在Build Phases -> Copy Bundle Resources下点击加号(+)

2.SDWebImage

4.0.0以前

SDWebImage 提供了一个分类 'UIImage+GIF' 来实现加载GIF图
不能直接使用sd_setImageWithURL:placeholderImage . 这个方法是不能播放本地Gif的,它只能显示Gif的第一张图片而已。

UIImage+GIF.h

@interface UIImage (GIF)
+ (UIImage *)sd_animatedGIFNamed:(NSString *)name;
+ (UIImage *)sd_animatedGIFWithData:(NSData *)data;
- (UIImage *)sd_animatedImageByScalingAndCroppingToSize:(CGSize)size;
@end

本地GIF图代码实现

NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"gif"];
NSData *data = [NSData dataWithContentsOfFile:path];
UIImage *image = [UIImage sd_animatedGIFWithData:data];
self.imageView.image = image;

网络GIF图 代码实现

NSURL *url = [NSURL URLWithString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1503467551868&di=f53202da5f5fbd6799c38da661d35ec6&imgtype=0&src=http%3A%2F%2Fscimg.jb51.net%2Fallimg%2F160622%2F2-1606221R445U9.gif"];
SDWebImageDownloader *downloader = [SDWebImageDownloader sharedDownloader];
[downloader downloadImageWithURL:url options:0 progress:^(NSInteger receivedSize, NSInteger               
        expectedSize, NSURL * _Nullable targetURL) {
        //下载进度
} completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error,
        BOOL finished) {
        UIImage *gifimage = [UIImage sd_animatedGIFWithData:data];
        self.imageView.image = gifimage;
}];
4.0.0以上

SDWebImage 在4.0版本 更改加载GIF图片的使用方法,修改了 UIImage+'GIF' 分类
将原有的三个方法删除掉两个, sd_animatedGIFWithData 也不能实现加载出GIF图片了,
新加方法 isGIF 判断图片是否是GIF图 (原理:判断UIImage的images参数是否为空)
UIImage+GIF.h

/**
 *  Compatibility method - creates an animated UIImage from an NSData, it will only contain the 1st frame image
* 翻译: 兼容性方法 - 从NSData创建动画UIImage,它只包含第一帧图像
 */
+ (UIImage *)sd_animatedGIFWithData:(NSData *)data;

/**
 *  Checks if an UIImage instance is a GIF. Will use the `images` array
* 翻译: 检查UIImage实例是否是GIF。 将使用`images`数组
 */
- (BOOL)isGIF;

从4.0版本开始 推出了 FLAnimatedImage 来实现加载GIF图
如果您使用Cocapods,请将 pod'SDWebImage / GIF' 添加到您的podfile中。
要使用它,只需确保使用FLAnimatedImageView而不是UIImageView。
注意:有一个向后兼容的功能,所以如果您仍然尝试将GIF加载到UIImageView中,它将仅将第一帧显示为静态图像。 详见:SDwebImage

FLAnimatedImage 使用

import <FLAnimatedImageView+WebCache.h>

FLAnimatedImageView 加载本地GIF图

FLAnimatedImageView *imageView = [[FLAnimatedImageView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
imageView.center = self.view.center;
[self.view addSubview:imageView];
NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"gif"];
NSData *data = [NSData dataWithContentsOfFile:path];
imageView.animatedImage = [FLAnimatedImage animatedImageWithGIFData:data];

加载网络GIF图

FLAnimatedImageView *imageView = [[FLAnimatedImageView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
imageView.center = self.view.center;
[self.view addSubview:imageView];
NSURL *url = [NSURL URLWithString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1503467551868&di=f53202da5f5fbd6799c38da661d35ec6&imgtype=0&src=http%3A%2F%2Fscimg.jb51.net%2Fallimg%2F160622%2F2-1606221R445U9.gif"];
[imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"test"]];

其中 animatedImage 是 FLAnimatedImage 的实例对象
导入 <FLAnimatedImageView+WebCache.h> 后 FLAnimatedImageView 加载URL GIF图片 与UIImageView加载URL图片并无太大区别

优缺点:

SDWebImage

容器都是大家熟悉的UIImageView,方便操作,还可以设置ContentModel
4.0以前还能是设置GIF图的duration (进入.m 找到关键字修改值即可)
4.0以后: 推出 FLAnimatedImage 类,拥有了更多的属性

4.0以前sd_animatedGIFWithData中duration的设定,这里是一个默认值,有需要可以修改 FLAnimatedImage.h 部分截图
webView

不能设置duration,也无法获取GIF图片的参数
容器为不常用的UIWebView 或者 WKWebView
无法更改图片的显示样式,只能改变控件的大小
方便快捷,不需要依赖第三方

建议:
有gif图,能给一个App添彩不少,但是我到目前为止还没有发现哪一个App中有大量的gif图展示的,一般项目中不会有大量的gif图存在(一些主要目的就是展示gif图的App除外),我自己的项目中也只是个别地方用到, GIF图一旦过多,肯定是影响性能的
简单快捷时 可以是使用webview ,需要操作GIF图时
建议使用 SDWebImage, 毕竟是一个大多数项目都依赖的第三方库
UITablView 建议使用 SDWebImage

结尾:

加载GIF图还有很多种方式,
例如直接使用原生框架<ImageIO>来实现, 太过复杂
CADisplayLink / CAKeyframeAnimation 都能实现,就是太过于麻烦
GIF图拆分 使用序列帧也可以 太过于耗性能
如果大家项目中需要用到序列帧来实现加载动画 建议使用:lottie-ios

相关文章

  • iOS加载GIF图

    1.WebView UIWebView WKWebView WKWebView实习和UIWebView一样,也是将...

  • Swift (四) gif图片播放

    @[TOC](IOS gif图片播放 swift) 1. GIF在iOS平台上的几种加载方式 使用Dispatch...

  • iOS-加载gif的四种方式

    iOS-加载gif的四种方式

  • Fresco Gif加载解析

    Fresco Gif加载解析 普通Gif加载 先看性能表现效果图: 从上面三张图可以看的出来普通的加载会频繁GC,...

  • UIImageView加载jpeg动图或gif动图

    IOS中,直接使用UIImage加载jpeg动图或gif动图,是没有动画效果的,可以使用YYKit框架里的YYIm...

  • 聊天列表显示gif总结

    TYAttributedLabel聊天列表显示gif总结;【聊天动态图,GIF图,iOS,TYAttributed...

  • Fresco Gif加载优化

    Fresco Gif加载优化 因为项目中需要用到加载Gif动图,而我们的图片加载框架用的就是Fresco,所以自然...

  • iOS加载gif动态背景

    官方没有直接加载gif动态图的控件,目前将gif文件转为Data类型,通过WKWebView加载Data类型数据,...

  • iOS加载本地GIF动图

    正常情况下,如果我们用UIImageView来直接加载本地GIF图片,运行显示图片并不是动态的,但是如果通过网络获...

  • GIF图加载

    一个加载gif的分类和弹窗的工具,有需要的自己拿。github.com/Will-ZJ/GIF

网友评论

    本文标题:iOS加载GIF图

    本文链接:https://www.haomeiwen.com/subject/eljddxtx.html