iOS-Gif圖片展示N種方式(原生+第三方)

來源:互聯網
上載者:User

iOS-Gif圖片展示N種方式(原生+第三方)

原生方法:

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 @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

////  UIImageView+Tool.m//  OneHelper////  Created by qiuxuewei on 16/3/2.//  Copyright ? 2016年 邱學偉. All rights reserved.//#import "UIImageView+Tool.h"//要引入ImageIO庫#import @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];}

第三方:
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];}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.