原文转自:http://www.2cto.com/kf/201603/491897.html
- iOS-Gif图片展示N种方式(原生+第三方)
-
原生方法:
1.UIWebView
特点:加载速度略长,性能更优,播放的gif动态图更加流畅。12345678910111213<code
class
=
"hljs objectivec"
>
//动态展示GIF图片-WebView
-(
void
)showGifImageWithWebView{
//读取gif图片数据
NSData *gifData = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@
"earthGif"
ofType:@
"gif"
]];
//UIWebView生成
UIWebView *imageWebView = [[UIWebView alloc] initWithFrame:CGRectMake(
112
,
302
,
132
,
102
)];
//用户不可交互
imageWebView.userInteractionEnabled = NO;
//加载gif数据
[imageWebView loadData:gifData MIMEType:@
"image/gif"
textEncodingName:nil baseURL:nil];
//视图添加此gif控件
[self.view addSubview:imageWebView];
}</code>
2.UIImagView
加载的方式更加快速,性能不如UIWebView,优点:易于扩展1)
增加一个UIImageView的类别(category),增加两个方法
UIImage+Tool
.h1234567891011<code
class
=
"hljs java"
>#
import
<uikit uikit.h=
""
>
@interface
UIImageView (Tool)
/** 解析gif文件数据的方法 block中会将解析的数据传递出来 */
-(
void
)getGifImageWithUrk:(NSURL *)url returnData:(
void
(^)(NSArray<uiimage> * imageArray,NSArray<nsnumber>*timeArray,CGFloat totalTime, NSArray<nsnumber>* widths, NSArray<nsnumber>* heights))dataBlock;
/** 为UIImageView添加一个设置gif图内容的方法: */
-(
void
)yh_setImage:(NSURL *)imageUrl;
@end
</nsnumber></nsnumber></nsnumber></uiimage></uikit></code>
.m
12345678910111213141516171819202122232425262728293031323334353637383940<code
class
=
"hljs objectivec"
>
//
// UIImageView+Tool.m
// OneHelper
//
// Created by qiuxuewei on 16/3/2.
// Copyright ? 2016年 邱学伟. All rights reserved.
//
#
import
"UIImageView+Tool.h"
//要引入ImageIO库
#
import
<imageio imageio.h=
""
>
@implementation
UIImageView (Tool)
//解析gif文件数据的方法 block中会将解析的数据传递出来
-(
void
)getGifImageWithUrk:(NSURL *)url returnData:(
void
(^)(NSArray<uiimage> * imageArray, NSArray<nsnumber>*timeArray,CGFloat totalTime, NSArray<nsnumber>* widths,NSArray<nsnumber>* heights))dataBlock{
//通过文件的url来将gif文件读取为图片数据引用
CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
//获取gif文件中图片的个数
size_t count = CGImageSourceGetCount(source);
//定义一个变量记录gif播放一轮的时间
float
allTime=
0
;
//存放所有图片
NSMutableArray * imageArray = [[NSMutableArray alloc]init];
//存放每一帧播放的时间
NSMutableArray * timeArray = [[NSMutableArray alloc]init];
//存放每张图片的宽度 (一般在一个gif文件中,所有图片尺寸都会一样)
NSMutableArray * widthArray = [[NSMutableArray alloc]init];
//存放每张图片的高度
NSMutableArray * heightArray = [[NSMutableArray alloc]init];
//遍历
for
(size_t i=
0
; i<count; __bridge=
""
__self=
"self;"
__weak=
""
cgfloat=
""
cgimageref=
""
getgifimagewithurk:imageurl=
""
height=
"[[info"
heightarray=
""
id=
""
image=
"CGImageSourceCreateImageAtIndex(source,"
imagearray=
""
info=
"(__bridge"
nsdictionary=
""
nsnumber=
""
nsstring=
""
nsurl=
""
self=
""
time=
"[[timeDic"
timearray=
""
timedic=
"[info"
uiimage=
""
width=
"[[info"
widtharray=
""
> *imageArray, NSArray<nsnumber> *timeArray, CGFloat totalTime, NSArray<nsnumber> *widths, NSArray<nsnumber> *heights) {
//添加帧动画
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@
"contents"
];
NSMutableArray * times = [[NSMutableArray alloc]init];
float
currentTime =
0
;
//设置每一帧的时间占比
for
(
int
i=
0
; i<imagearray.count; __self=
""
addanimation:animation=
""
animation=
""
animation.duration=
"totalTime;"
animation.repeatcount=
"MAXFLOAT;"
camediatimingfunction=
""
code=
""
end=
""
nsnumber=
""
numberwithfloat:currenttime=
""
times=
""
uiimageview=
""
></imagearray.count;></nsnumber></nsnumber></nsnumber></count;></nsnumber></nsnumber></nsnumber></uiimage></imageio></code>
在加载gif的地方使用
导入 UIImageView+Tool12345678<code
class
=
"hljs objectivec"
><code
class
=
"hljs objectivec"
>-(
void
)showGifImageWithImageView{
UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(
112
,
342
,
132
,
102
)];
NSURL * url = [[NSURL alloc]initFileURLWithPath:[[NSBundle mainBundle] pathForResource:@
"earthGif.gif"
ofType:nil]];
[imageView yh_setImage:url];
[self.view addSubview:imageView];
}</code></code>
第三方:
1.YLGIFImage
github链接: https://github.com/liyong03/YLGIFImage