gif图片是非常常见的图片格式,尤其是在聊天的过程中,gif表情使用地很频繁。但是ios竟然没有现成的支持加载和播放gif的类。
简单地汇总了一下,大概有以下几种方法:
一、加载本地gif文件
1、使用uiwebview
?| 1 2 3 4 5 6 7 8 9 10 11 12 |
// 读取gif图片数据
uiwebview *webview = [[uiwebview alloc] initwithframe:cgrectmake(0,0,200,200)];
[self.view addsubview:webview];
nsstring *path = [[nsbundle mainbundle] pathforresource:@"001" oftype:@"gif"];
/*
nsdata *data = [nsdata datawithcontentsoffile:path];
使用loaddata:mimetype:textencodingname: 则有警告
[webview loaddata:data mimetype:@"image/gif" textencodingname:nil baseurl:nil];
*/
nsurl *url = [nsurl urlwithstring:path];
[webview loadrequest:[nsurlrequest requestwithurl:url]];
|
但是使用uiwebview的弊端在于,不能设置gif动画的播放时间。
2、将gif拆分成多张图片,使用uiimageview播放
最好把所需要的gif图片打包到bundle文件内,如下图所示

| 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 |
- (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 = 0; //动画重复次数
[gifimageview startanimating];
[self.view addsubview:gifimageview];
}
|
3、使用sdwebimage
但是很遗憾,sdwebimage 的 sd_setimagewithurl:placeholderimage:这个方法是不能播放本地gif的,它只能显示gif的第一张图片而已。so,此方法行不通
?| 1 2 |
uiimageview *gifimageview = [[uiimageview alloc] initwithframe:frame];
[gifimageview sd_setimagewithurl:nil placeholderimage:[uiimage imagenamed:@"giftest.gif"]];
|
其实,在sdwebimage这个库里有一个uiimage+gif的类别,里面为uiimage扩展了三个方法:
?| 1 2 3 4 5 |
@interface uiimage (gif)
+ (iimage *)sd_animatedgifnamed:(nsstring *)name;
+ (uiimage *)sd_animatedgifwithdata:(nsdata *)data;
- (uiimage *)sd_animatedimagebyscalingandcroppingtosize:(cgsize)size;
@end
|
大家一看就知道,我们要获取处理后的gif图片,其实只要调用前面两个中的其中一个方法就行了
注意:第一个只需要传gif的名字,而不需要带扩展名(如gif图片名字为001@2x.gif,只需传001即可)
我们就使用第二个方法试一试效果:
?| 1 2 3 4 |
nsstring *path = [[nsbundle mainbundle] pathforresource:@"giftest" oftype:@"gif"];
nsdata *data = [nsdata datawithcontentsoffile:path];
uiimage *image = [uiimage sd_animatedgifwithdata:data];
gifimageview.image = image;
|
然后通过断点,我们看下获取到的image是个什么样的东东:

我们发现:
image的isa指针指向了_uianimatedimage ,说明它是一个叫作_uianimatedimage 的类(当然,这个_uianimatedimage 苹果是不会直接让我们使用的)
_images 表示:这个gif包含了多少张图片
_duration表示:执行一次完整动画所需的时长
其实,动画执续时间_duration也可以更改!
我们来看下此方法的内部实现:
?| 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 |
+ (uiimage *)sd_animatedgifwithdata:(nsdata *)data {
if (!data) {
return nil;
}
cgimagesourceref source = cgimagesourcecreatewithdata((__bridge cfdataref)data, null);
size_t count = cgimagesourcegetcount(source);
uiimage *animatedimage;
if (count <= 1) {
animatedimage = [[uiimage alloc] initwithdata:data];
}
else {
nsmutablearray *images = [nsmutablearray array];
nstimeinterval duration = 0.0f;
for (size_t i = 0; i < count; i++) {
cgimageref image = cgimagesourcecreateimageatindex(source, i, null);
duration += [self sd_framedurationatindex:i source:source];
[images addobject:[uiimage imagewithcgimage:image scale:[uiscreen mainscreen].scale orientation:uiimageorientationup]];
cgimagerelease(image);
}
if (!duration) {
duration = (1.0f / 10.0f) * count;
}
animatedimage = [uiimage animatedimagewithimages:images duration:duration];
}
cfrelease(source);
return animatedimage;
}
|
很明显,duration是可以随意更改的,只不过此方法设置了一个默认值
(duration = (1.0f / 10.0f) * count)
归根到底,创建新的动态的image其实是调用了系统提供的一个uiimage的类方法而已:
?| 1 |
uiimage *animatedimage = [uiimage animatedimagewithimages:images duration:duration];
|
二、加载网络gif文件
加载网络的gif文件就简单多了。最简单的方法,我们只需要使用sdwebimage 的 sd_setimagewithurl:这个方法传入gif文件是url地址即可。
纠其原因:稍微仔细看了sdwebimage内部实现就可以清楚,大概是以下几个步骤:
1、sdwebimage根据url将gif文件下载下来,格式为一个nsdata
2、如果判断是gif格式,则会调用** sd_animatedgifwithdata:** 将data转换成我们需要的gif格式
3、通过上面的方法二即可显示出gif图片
| 1 2 |
uiimage *image = [uiimage sd_animatedgifwithdata:data];
gifimageview.image = image;
|
总结
一、加载本地gif文件
1、使用uiwebview不可以设置duration,其他两种方法都可设置。而且方法1的容器为uiwebview ,其余两种的容器都是大家熟悉的uiimageview
2、方法2和方法3需要对应看应用场景
如:下拉、上拉加载控件需要一个根据拉动距离设置特定的image,则需要使用方法2
直接显示gif图片,则使用方法3会更方便
二、加载网络gif文件
直接使用sdwebimage 的 sd_setimagewithurl:这个方法传入gif文件是url地址即可
ps:简单小demo
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://www.jianshu.com/p/f0530a75c7af








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