温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

IOS中如何实现播放gif动态图

发布时间:2021-08-04 10:53:19 来源:亿速云 阅读:626 作者:小新 栏目:移动开发

这篇文章给大家分享的是有关IOS中如何实现播放gif动态图的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

一、引言

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

二、为原生的UIImageView添加类别来支持gif动态图的播放

     gif动态图文件中包含了一组图片及其信息,信息主要记录着每一帧图片播放的时间,我们如果获取到了gif文件中所有的图片同时又获取到每一帧图片播放的时间,就可以为UIImageView添加核心动画的方法来让其播放gif的内容了。

    首先解析gif文件中的数据,代码如下:

//要引入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图内容的方法:

-(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"];
  }];
}

使用代码示例如下:

 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动态图

三、使用UIWebView来加载gif动态图数据

    iOS中的UIWebView功能十分强大,可以通过UIWebView为载体,来展示gif图。并且这种方法也十分简单,代码如下:

 //读取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能够更加自由的让开发者进行扩展。

感谢各位的阅读!关于“IOS中如何实现播放gif动态图”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI