在iOS开发中,UIImageView类专门来负责图片数据的渲染,而且UIImageView也有帧动画的方法来播放一组图片,可是对于gif类型的数据,UIImageView中并无现成的接口提供给开发者使用,在iOS中通常能够经过两种方式来播放gif动态图,一种方式是经过ImageIO框架中的方法将gif文件中的数据进行解析,再使用coreAnimation核心动画来播放gif动画,另外一种方式计较简单,能够直接经过webView来渲染gif图。web
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中的UIWebView功能十分强大,能够经过UIWebView为载体,来展现gif图。而且这种方法也十分简单,代码以下:url
//读取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];
通过测试,从加载速度上来讲,经过UIImageView类别加载的方式更加快速,UIWebView的方式加载时间会稍长,可是从性能上来比较,WebView的方式性能更优,播放的gif动态图更加流畅。在开发中,能够根据需求,适当选择,例如虽然WebView加载的方式性能更好,可是在许多状况下,原生的UIImageView可以更加自由的让开发者进行扩展。spa
专一技术,热爱生活,交流技术,也作朋友。code
——珲少 QQ群:203317592接口