iOS中gif图的显示方法示例

时间:2022-02-02 21:43:52

一、前言

ios开发中,大部分时候我们显示一张静态图就可以了,但是有的时候为了ui表现更生动,我就有可能需要展示gif图来达到效果了。

网上找了一下,显示gif图的框架找到了两个。

  • sdwebimage
  • yyimage

iOS中gif图的显示方法示例

二、显示本地gif图

sdwebimage和yyimage的显示本地图片代码。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//load loacle gif image
- (void)loadlocalegifimage{
 
 //sdwebimage
 [self labelfactorywithframe:cgrectmake(0, 80, kscreenwidth, 20) title:@"sdwebimage"];
 nsstring *path = [[nsbundle mainbundle] pathforresource:@"test" oftype:@"gif"];
 nsdata *gifdata = [nsdata datawithcontentsoffile:path];
 uiimageview *sdimageview = [[uiimageview alloc] initwithframe:cgrectmake(0, 100, kscreenwidth, kscreenheight/3)];
 sdimageview.image = [uiimage sd_animatedgifwithdata:gifdata];
 [self.view addsubview:sdimageview];
 
 //yyimage show gif image
 [self labelfactorywithframe:cgrectmake(0, kscreenheight/2 - 20, kscreenwidth, 20) title:@"yyimage"];
 yyimage *yyimage = [yyimage imagenamed:@"test.gif"];
 yyanimatedimageview *yyimageview = [[yyanimatedimageview alloc] initwithimage:yyimage];
 yyimageview.frame = cgrectmake(0, kscreenheight/2, kscreenwidth, kscreenheight/3);
 [self.view addsubview:yyimageview];
}

三、加载网络的gif图

sdwebimage和yyimage的加载网络图片代码。

?
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
//download network gif image
- (void)downloadnetworkgifimage{
 
 //sdwebimage
 [self labelfactorywithframe:cgrectmake(0, 80, kscreenwidth, 20) title:@"sdwebimage"];
 flanimatedimageview *sdimageview = [[flanimatedimageview alloc] initwithframe:cgrectmake(0, 100, kscreenwidth, kscreenheight/3)];
 [sdimageview sd_setimagewithurl:[nsurl urlwithstring:@"http://photocdn.sohu.com/20151214/mp48444247_1450092561460_10.gif"]];
 [self.view addsubview:sdimageview];
 
 //yyimage show gif image
 [self labelfactorywithframe:cgrectmake(0, kscreenheight/2 - 20, kscreenwidth, 20) title:@"yyimage"];
 yyimage *yyimage = [yyimage imagewithdata:[nsdata datawithcontentsofurl:[nsurl urlwithstring:@"http://photocdn.sohu.com/20151214/mp48444247_1450092561460_10.gif"]]];
 yyanimatedimageview *yyimageview = [[yyanimatedimageview alloc] initwithimage:yyimage];
 yyimageview.frame = cgrectmake(0, kscreenheight/2, kscreenwidth, kscreenheight/3);
 [self.view addsubview:yyimageview];
}
 
- (void)labelfactorywithframe:(cgrect)frame title:(nsstring *)title{
 
 uilabel *label = [[uilabel alloc] initwithframe:frame];
 label.textalignment = nstextalignmentcenter;
 label.textcolor = [uicolor blackcolor];
 label.font = [uifont systemfontofsize:14];
 label.text = title;
 [self.view addsubview:label];
}

四、podfile文件内容

?
1
2
3
4
5
6
7
8
9
10
platform :ios, '10.0'
inhibit_all_warnings!
 
target 'gifdemo' do
 
pod 'yyimage'
pod 'sdwebimage/gif'
pod 'flanimatedimage'
 
end

五、没有demo的文章不是好文章

sdwebimage和yyimage框架显示本地和网络gif图的demo传送门

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对服务器之家的支持。

原文链接:https://segmentfault.com/a/1190000019473716