iOS-加載gif圖片的幾種方式

1. 原生方法:


  1. UIWebView特點:加載速度略長,性能更優(yōu),播放的gif動態(tài)圖更加流暢。
//動態(tài)展示GIF圖片-WebView
-(void)showGifImageWithWebView{
    //讀取gif圖片數(shù)據(jù)
    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數(shù)據(jù)
    [imageWebView loadData:gifData MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
    //視圖添加此gif控件
    [self.view addSubview:imageWebView];
}
  1. UIImagView加載的方式更加快速,性能不如UIWebView,優(yōu)點:易于擴(kuò)展
    增加一個UIImageView的類別(category),增加兩個方法
    UIImage+Tool.h
#import <UIKit/UIKit.h>
@interface UIImageView (Tool)
/** 解析gif文件數(shù)據(jù)的方法 block中會將解析的數(shù)據(jù)傳遞出來 */
-(void)getGifImageWithUrk:(NSURL *)url returnData:(void(^)(NSArray<UIImage *> * imageArray,NSArray<NSNumber *>*timeArray,CGFloat totalTime, NSArray<NSNumber *>* widths, NSArray<NSNumber *>* heights))dataBlock;
/** 為UIImageView添加一個設(shè)置gif圖內(nèi)容的方法: */
-(void)yh_setImage:(NSURL *)imageUrl;
@end

.m

@implementation UIImageView (Tool)
//解析gif文件數(shù)據(jù)的方法 block中會將解析的數(shù)據(jù)傳遞出來
-(void)getGifImageWithUrk:(NSURL *)url returnData:(void(^)(NSArray<UIImage *> * imageArray, NSArray<NSNumber *>*timeArray,CGFloat totalTime, NSArray<NSNumber *>* widths,NSArray<NSNumber *>* heights))dataBlock{
    //通過文件的url來將gif文件讀取為圖片數(shù)據(jù)引用
    CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
    //獲取gif文件中圖片的個數(shù)
    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添加一個設(shè)置gif圖內(nèi)容的方法:
-(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;
        //設(shè)置每一幀的時間占比
        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]];
        //設(shè)置循環(huán)
        animation.repeatCount= MAXFLOAT;
        //設(shè)置播放總時長
        animation.duration = totalTime;
        //Layer層添加
        [[(UIImageView *)__self layer]addAnimation:animation forKey:@"gifAnimation"];
    }];
}
@end

在加載gif的地方使用導(dǎo)入 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 轉(zhuǎn) NSData
    //Gif 路徑
    NSString *pathForFile = [[NSBundle mainBundle] pathForResource: @"earthGif" ofType:@"gif"];
    //轉(zhuǎn)成NSData
    NSData *dataOfGif = [NSData dataWithContentsOfFile: pathForFile];
    //初始化FLAnimatedImage對象
    FLAnimatedImage *image = [FLAnimatedImage animatedImageWithGIFData:dataOfGif];
    //初始化FLAnimatedImageView對象
    FLAnimatedImageView *imageView = [[FLAnimatedImageView alloc] init];
    //設(shè)置GIF圖片
    imageView.animatedImage = image;
    imageView.frame = CGRectMake(112, 342, 132, 102);
    [self.view addSubview:imageView];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1.系統(tǒng)UIImageView 多張圖片組成動畫 /** * UIImageView 動畫 * Memor...
    zhengelababy閱讀 9,284評論 3 6
  • 在iOS中隨處都可以看到絢麗的動畫效果,實現(xiàn)這些動畫的過程并不復(fù)雜,今天將帶大家一窺ios動畫全貌。在這里你可以看...
    每天刷兩次牙閱讀 8,698評論 6 30
  • 在iOS中隨處都可以看到絢麗的動畫效果,實現(xiàn)這些動畫的過程并不復(fù)雜,今天將帶大家一窺iOS動畫全貌。在這里你可以看...
    F麥子閱讀 5,274評論 5 13
  • //設(shè)置尺寸為屏幕尺寸的時候self.window = [[UIWindow alloc] initWithFra...
    LuckTime閱讀 985評論 0 0
  • 第一種方法 第二種方法 第三種方法
    Vector_Wings閱讀 1,934評論 0 0

友情鏈接更多精彩內(nèi)容