iOS加載Gif圖片的N種方式

1.系統UIImageView 多張圖片組成動畫

/** ? * ? UIImageView 動畫 ? * ? Memory-23M ? */ ?

-(void)gifPlay1 ?{?

?// ? ?NSArray ?*array=@[@"image0.png",@"image1.png",@"image2.png"]; ?

// ? ?UIImageView ?*imgview= [UIImageView imageViewAnimation:CGRectMake(50,80, 550/2, 200) ?imageNames:array duration:1]; ? ? ? ? ?

??UIImageView* animatedImageView = [[UIImageView alloc] initWithFrame:CGRectMake(50,80, 550/2, 200)]; ? ? ?animatedImageView.animationImages =@[[UIImage imageNamed:@"image0"], ? ? [UIImage imageNamed:@"image1"], ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [UIImage imageNamed:@"image2"] ?]; ? ??

?animatedImageView.animationDuration = 1.0f; ? ? ?

animatedImageView.animationRepeatCount = 0; ? ?

??[self.view addSubview: animatedImageView]; ? ??

?[animatedImageView startAnimating];?

?}




2.利用第三方庫

1)IImageView-PlayGIF ?YFGIFImageView

/** ? * ?UIImageView-PlayGIF 是 UIImageView 子類,用來顯示 GIF。UIIMageView-PlayGIF 性能高,而且占用的內存很低。?

? * ?https://github.com/yfme/UIImageView-PlayGIF ? * ?Memory-21.9M ? * ?

#import "YFGIFImageView.h" ?

?*/ ?

-(void)gifPlay2 ?{ ?

?? ?NSString ?*gifPath=[[NSBundle mainBundle] pathForResource:@"test" ofType:@"gif"];?

? ? ?YFGIFImageView ?*gifview=[[YFGIFImageView alloc]init]; ? ?

??gifview.backgroundColor=[UIColor clearColor]; ?

?? ?gifview.gifPath=gifPath; ?

?? ?gifview.frame=CGRectMake(50, 100,550/2, 200);?

? ? ?[self.view addSubview:gifview];?

? ? ?[gifview startGIF];?

?}


2)SCGIFImageView


/** ? * ?摘要: SCGIFImageView是一個開源的GIF圖片動畫顯示控件,通過將GIF的每一幀都取出來生成UIImage對象存放在一個數組中,然后使用NSTimer進行動畫輪轉。 ??

* ?https://github.com/shichangone/SCGifExample ? * ?Memory-22.5M ? *

??#import "SCGIFImageView.h" ??

*/

??-(void)gifPlay3 ?{ ?

?? ?NSString* filePath = [[NSBundle mainBundle] pathForResource:@"test.gif" ofType:nil]; ?

?? ?NSData* imageData = [NSData dataWithContentsOfFile:filePath]; ?

?? ?SCGIFImageView* gifImageView = [[SCGIFImageView alloc]init]; ??

? ?[gifImageView setData:imageData]; ? ?

??gifImageView.frame = CGRectMake(50,100, gifImageView.image.size.width, gifImageView.image.size.height); ? ? ?[self.view addSubview:gifImageView];?

?}


3)YLGIFImage

/** ? * ?YLGIFImage 是異步 GIF 圖像解碼器和圖像查看器,支持播放 GIF 圖像,而且使用很少的內存。

?? * ?https://github.com/liyong03/YLGIFImage ? * ?Memory-22.7M ??

* #import "YLImageView.h" ??

* ?#import "YLGIFImage.h" ?

?*/ ?

-(void)gifPlay5 ?{ ?

?? ?YLImageView* imageView = [[YLImageView alloc] initWithFrame:CGRectMake(0, 160, 320, 240)];?

? ? ?[self.view addSubview:imageView]; ??

? ?imageView.image = [YLGIFImage imageNamed:@"test.gif"]; ?

}



4)SDWebImageView里的UIImage+GIF

提供接口:

+ (UIImage *)sd_animatedGIFNamed:(NSString *)name;

+ (UIImage *)sd_animatedGIFWithData:(NSData *)data;

- (UIImage *)sd_animatedImageByScalingAndCroppingToSize:(CGSize)size;


/** ? * ?利用SDWebImageView 庫播放gif ? * ?Memory-22.6M ? *?

?#import "UIImage+GIF.h" ?

?*/?

?-(void)gifPlay6 ?{ ?

?? ?UIImage ?*image=[UIImage sd_animatedGIFNamed:@"test"]; ? ??

?UIImageView ?*gifview=[[UIImageView alloc]initWithFrame:CGRectMake(50,80,image.size.width, image.size.height)]; ? ? ?gifview.backgroundColor=[UIColor orangeColor]; ?

?? ?gifview.image=image;

?? ? ?[self.view addSubview:gifview]; ?

}

5)為MBProgressHUD 添加加載動畫

/** ? * ?MBProgressHUD 添加加載動畫 ? * ?Memory-23.8M ? *?

?#import "UIImage+GIF.h" ?

?* ?#import "MBProgressHUD.h" ??

*/ ?

-(void)gifPlay6 ?{ ?

?? ?UIImage ?*image=[UIImage sd_animatedGIFNamed:@"test"]; ??

? ?UIImageView ?*gifview=[[UIImageView alloc]initWithFrame:CGRectMake(0,0,image.size.width/2, image.size.height/2)]; ?

?? ?gifview.image=image; ? ? ? ?

?? ?MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; ? ?

??hud.color=[UIColor grayColor];//默認顏色太深了 ??

? ?hud.mode = MBProgressHUDModeCustomView; ? ?

??hud.labelText = @"加載中..."; ? ?

??hud.customView=gifview;?

?}



3.為原生的UIImageView添加類別來支持gif動態圖的播放

?gif動態圖文件中包含了一組圖片及其信息,信息主要記錄著每一幀圖片播放的時間,我們如果獲取到了gif文件中所有的圖片同時又獲取到每一幀圖片播放的時間,就可以為UIImageView添加核心動畫的方法來讓其播放gif的內容了。

? ?首先解析gif文件中的數據,代碼如下:

//要引入ImageIO庫

#import

//解析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

? ? ? ?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 *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

? ? ? ? ? ? ? ?[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];

三、使用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能夠更加自由的讓開發者進行擴展。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容