gpt4 book ai didi

浅析IOS中播放gif动态图的方法

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章浅析IOS中播放gif动态图的方法由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

1、引言 。

    在ios开发中,uiimageview类专门来负责图片数据的渲染,并且uiimageview也有帧动画的方法来播放一组图片,但是对于gif类型的数据,uiimageview中并没有现成的接口提供给开发者使用,在ios中一般可以通过两种方式来播放gif动态图,一种方式是通过imageio框架中的方法将gif文件中的数据进行解析,再使用coreanimation核心动画来播放gif动画,另一种方式计较简单,可以直接通过webview来渲染gif图.

2、为原生的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];

浅析IOS中播放gif动态图的方法

3、使用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];

4、两种加载gif动态图方式的优劣 。

    经过测试,从加载速度上来说,通过uiimageview类别加载的方式更加快速,uiwebview的方式加载时间会稍长,但是从性能上来比较,webview的方式性能更优,播放的gif动态图更加流畅。在开发中,可以根据需求,适当选择,例如虽然webview加载的方式性能更好,但是在许多情况下,原生的uiimageview能够更加自由的让开发者进行扩展.

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持! 。

原文链接:https://my.oschina.net/u/2340880/blog/608560 。

最后此篇关于浅析IOS中播放gif动态图的方法的文章就讲到这里了,如果你想了解更多关于浅析IOS中播放gif动态图的方法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com