原生方法:

1.UIWebView

特点:载入速度略长,性能更优。播放的gif动态图更加流畅。

//动态展示GIF图片-WebView
-(void)showGifImageWithWebView{
//读取gif图片数据
NSData *gifData = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"earthGif" ofType:@"gif"]];
//UIWebView生成
UIWebView *imageWebView = [[UIWebView alloc] initWithFrame:CGRectMake(112, 302, 132, 102)];
//用户不可交互
imageWebView.userInteractionEnabled = NO;
//载入gif数据
[imageWebView loadData:gifData MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
//视图加入此gif控件
[self.view addSubview:imageWebView];
}

2.UIImagView

载入的方式更加高速,性能不如UIWebView,长处:易于扩展

1)

添加一个UIImageView的类别(category),添加两个方法

UIImage+Tool

.h

#import <UIKit/UIKit.h>

@interface UIImageView (Tool)

/** 解析gif文件数据的方法 block中会将解析的数据传递出来 */
-(void)getGifImageWithUrk:(NSURL *)url returnData:(void(^)(NSArray<UIImage *> * imageArray,NSArray<NSNumber *>*timeArray,CGFloat totalTime, NSArray<NSNumber *>* widths, NSArray<NSNumber *>* heights))dataBlock; /** 为UIImageView加入一个设置gif图内容的方法: */
-(void)yh_setImage:(NSURL *)imageUrl; @end

.m

//
// UIImageView+Tool.m
// OneHelper
//
// Created by qiuxuewei on 16/3/2.
// Copyright © 2016年 邱学伟. All rights reserved.
// #import "UIImageView+Tool.h"
//要引入ImageIO库
#import <ImageIO/ImageIO.h> @implementation UIImageView (Tool) //解析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]];
}
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"];
}];
} @end

在载入gif的地方使用

导入 UIImageView+Tool

-(void)showGifImageWithImageView{

    UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(112, 342, 132, 102)];
NSURL * url = [[NSURL alloc]initFileURLWithPath:[[NSBundle mainBundle] pathForResource:@"earthGif.gif" ofType:nil]];
[imageView yh_setImage:url];
[self.view addSubview:imageView]; }

第三方:

1.YLGIFImage

github链接: https://github.com/liyong03/YLGIFImage

#import "YLGIFImage.h"
#import "YLImageView.h" -(void)showGifImageWithYLImageView{
YLImageView* imageView = [[YLImageView alloc] initWithFrame:CGRectMake(112, 342, 132, 102)];
CGFloat centerX = self.view.center.x;
[imageView setCenter:CGPointMake(centerX, 402)];
[self.view addSubview:imageView];
imageView.image = [YLGIFImage imageNamed:@"earthGif.gif"];
}

2.FLAnimatedImage

github链接:https://github.com/Flipboard/FLAnimatedImage

-(void)showGifImageWithFLAnimatedImage{
//GIF 转 NSData
//Gif 路径
NSString *pathForFile = [[NSBundle mainBundle] pathForResource: @"earthGif" ofType:@"gif"];
//转成NSData
NSData *dataOfGif = [NSData dataWithContentsOfFile: pathForFile];
//初始化FLAnimatedImage对象
FLAnimatedImage *image = [FLAnimatedImage animatedImageWithGIFData:dataOfGif];
//初始化FLAnimatedImageView对象
FLAnimatedImageView *imageView = [[FLAnimatedImageView alloc] init];
//设置GIF图片
imageView.animatedImage = image;
imageView.frame = CGRectMake(112, 342, 132, 102);
[self.view addSubview:imageView];
}

最新文章

  1. 01背包问题python 2.7实现
  2. ASM:《X86汇编语言-从实模式到保护模式》第12章:存储器的保护
  3. Node.js 手册查询-4-Express 方法
  4. Object类概述
  5. xss-跨站脚本攻击-后台传给前端的html标签安全显示
  6. POJ 1077 &amp;&amp; HDU 1043 Eight A*算法,bfs,康托展开,hash 难度:3
  7. 剑指OFFER之把数组排成最小的数(九度OJ1504)
  8. 编译安装nginx并修改版本头信息—参考实例
  9. JavaScript 函数作用域和闭包
  10. 写具有良好风格的ABAP代码
  11. .NET链接Oracle 参数绑定问题
  12. Mybatis 系列1
  13. django之ModelForm的用法
  14. How to create your iOS team provisioning profile ?
  15. 2018.10.25 bzoj3928: [Cerc2014] Outer space invaders(区间dp)
  16. c++ #ifdef的用法
  17. 【Unity】3.6 导入图片资源
  18. HttpClient4.5简单使用
  19. 《EMCAScript6入门》读书笔记——2.let和const命令
  20. 排序 permutation

热门文章

  1. python aiohttp sancio 框架性能测试
  2. JavaScript中的局部作用域及常量的定义
  3. 【母函数】hdu2082 找单词
  4. 【bzoj1604】【[Usaco2008 Open]Cow Neighborhoods】简单的谈谈曼哈顿距离
  5. [TC-HouseProtection]House Protection
  6. Scala实战高手****第13课:Scala模式匹配实战和Spark源码鉴赏
  7. iOS开发之指定UIView的某几个角为圆角
  8. iOS开发 Swift开发数独游戏(五)显示游戏答案
  9. JSON.parse(str),JSON.stringify(a)
  10. Oracle Service Bus中的线程