iOS-加载gif图片的几种方式

1. 原生方法:


  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];
}
  1. UIImagView加载的方式更加快速,性能不如UIWebView,优点:易于扩展
    增加一个UIImageView的类别(category),增加两个方法
    UIImage+Tool.h
#import 
@interface UIImageView (Tool)
/** 解析gif文件数据的方法 block中会将解析的数据传递出来 */
-(void)getGifImageWithUrk:(NSURL *)url returnData:(void(^)(NSArray * imageArray,NSArray*timeArray,CGFloat totalTime, NSArray* widths, NSArray* heights))dataBlock;
/** 为UIImageView添加一个设置gif图内容的方法: */
-(void)yh_setImage:(NSURL *)imageUrl;
@end

.m

@implementation UIImageView (Tool)
//解析gif文件数据的方法 block中会将解析的数据传递出来
-(void)getGifImageWithUrk:(NSURL *)url returnData:(void(^)(NSArray * imageArray, NSArray*timeArray,CGFloat totalTime, NSArray* widths,NSArray* 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 *imageArray, NSArray *timeArray, CGFloat totalTime, NSArray *widths, NSArray *heights) {
        //添加帧动画
        CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];
        NSMutableArray * times = [[NSMutableArray alloc]init];
        float currentTime = 0;
        //设置每一帧的时间占比
        for (int i=0; i

在加载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];
}

2. 第三方:


  1. YLGIFImagegithub
#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"];
}
  1. FLAnimatedImagegithub
-(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];
}

你可能感兴趣的:(iOS-加载gif图片的几种方式)