一、引言
在ios开发中,uiimageview类专门来负责图片数据的渲染,并且uiimageview也有帧动画的方法来播放一组图片,但是对于gif类型的数据,uiimageview中并没有现成的接口提供给开发者使用,在ios中一般可以通过两种方式来播放gif动态图,一种方式是通过imageio框架中的方法将gif文件中的数据进行解析,再使用coreanimation核心动画来播放gif动画,另一种方式计较简单,可以直接通过webview来渲染gif图。
二、为原生的uiimageview添加类别来支持gif动态图的播放
gif动态图文件中包含了一组图片及其信息,信息主要记录着每一帧图片播放的时间,我们如果获取到了gif文件中所有的图片同时又获取到每一帧图片播放的时间,就可以为uiimageview添加核心动画的方法来让其播放gif的内容了。
首先解析gif文件中的数据,代码如下:
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
//要引入imageio库
#import <imageio/imageio.h>
//解析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; i++) {
cgimageref image = cgimagesourcecreateimageatindex(source, i, null);
[imagearray addobject:(__bridge uiimage *)(image)];
cgimagerelease(image);
//获取图片信息
nsdictionary * info = (__bridge nsdictionary*)cgimagesourcecopypropertiesatindex(source, i, null);
cgfloat width = [[info objectforkey:(__bridge nsstring *)kcgimagepropertypixelwidth] floatvalue];
cgfloat height = [[info objectforkey:(__bridge nsstring *)kcgimagepropertypixelheight] floatvalue];
[widtharray addobject:[nsnumber numberwithfloat:width]];
[heightarray addobject:[nsnumber numberwithfloat:height]];
nsdictionary * timedic = [info objectforkey:(__bridge nsstring *)kcgimagepropertygifdictionary];
cgfloat time = [[timedic objectforkey:(__bridge nsstring *)kcgimagepropertygifdelaytime]floatvalue];
alltime+=time;
[timearray addobject:[nsnumber numberwithfloat:time]];
cfrelease(info);
}
cfrelease(source);
datablock(imagearray,timearray,alltime,widtharray,heightarray);
}
|
为uiimageview添加一个设置gif图内容的方法:
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
-(void)yh_setimage:(nsurl *)imageurl{
__weak id __self = self;
[self getgifimagewithurk:imageurl returndata:^(nsarray<uiimage *> *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; i++) {
[times addobject:[nsnumber numberwithfloat:currenttime/totaltime]];
currenttime+=[timearray[i] floatvalue];
}
[animation setkeytimes:times];
[animation setvalues:imagearray];
[animation settimingfunction:[camediatimingfunction functionwithname:kcamediatimingfunctionlinear]];
//设置循环
animation.repeatcount= maxfloat;
//设置播放总时长
animation.duration = totaltime;
//layer层添加
[[(uiimageview *)__self layer]addanimation:animation forkey:@"gifanimation"];
}];
}
|
使用代码示例如下:
?| 1 2 3 4 |
uiimageview * imageview = [[uiimageview alloc]initwithframe:cgrectmake(0,0 , 320, 200)];
nsurl * url = [[nsurl alloc]initfileurlwithpath:[[nsbundle mainbundle] pathforresource:imagename oftype:nil]];
[imageview yh_setimage:url];
[self.view addsubview:imageview];
|

三、使用uiwebview来加载gif动态图数据
ios中的uiwebview功能十分强大,可以通过uiwebview为载体,来展示gif图。并且这种方法也十分简单,代码如下:
?| 1 2 3 4 5 6 7 8 9 10 |
//读取gif数据
nsdata *gifdata = [nsdata datawithcontentsofurl:imageurl];
uiwebview *webview = [[uiwebview alloc] initwithframe:cgrectmake(0, 0, self.frame.size.width, self.frame.size.height)];
//取消回弹效果
webview.scrollview.bounces=no;
webview.backgroundcolor = [uicolor clearcolor];
//设置缩放模式
webview.scalespagetofit = yes;
//用webview加载数据
[webview loaddata:gifdata mimetype:@"image/gif" textencodingname:nil baseurl:nil];
|
四、两种加载gif动态图方式的优劣
经过测试,从加载速度上来说,通过uiimageview类别加载的方式更加快速,uiwebview的方式加载时间会稍长,但是从性能上来比较,webview的方式性能更优,播放的gif动态图更加流畅。在开发中,可以根据需求,适当选择,例如虽然webview加载的方式性能更好,但是在许多情况下,原生的uiimageview能够更加自由的让开发者进行扩展。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
原文链接:https://my.oschina.net/u/2340880/blog/608560








发表评论
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。