国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

淺析IOS中播放gif動(dòng)態(tài)圖的方法

2020-07-26 02:53:38
字體:
供稿:網(wǎng)友

一、引言

    在iOS開發(fā)中,UIImageView類專門來負(fù)責(zé)圖片數(shù)據(jù)的渲染,并且UIImageView也有幀動(dòng)畫的方法來播放一組圖片,但是對(duì)于gif類型的數(shù)據(jù),UIImageView中并沒有現(xiàn)成的接口提供給開發(fā)者使用,在iOS中一般可以通過兩種方式來播放gif動(dòng)態(tài)圖,一種方式是通過ImageIO框架中的方法將gif文件中的數(shù)據(jù)進(jìn)行解析,再使用coreAnimation核心動(dòng)畫來播放gif動(dòng)畫,另一種方式計(jì)較簡(jiǎn)單,可以直接通過webView來渲染gif圖。

二、為原生的UIImageView添加類別來支持gif動(dòng)態(tài)圖的播放

     gif動(dòng)態(tài)圖文件中包含了一組圖片及其信息,信息主要記錄著每一幀圖片播放的時(shí)間,我們?nèi)绻@取到了gif文件中所有的圖片同時(shí)又獲取到每一幀圖片播放的時(shí)間,就可以為UIImageView添加核心動(dòng)畫的方法來讓其播放gif的內(nèi)容了。

    首先解析gif文件中的數(shù)據(jù),代碼如下:

//要引入ImageIO庫(kù)#import <ImageIO/ImageIO.h>//解析gif文件數(shù)據(jù)的方法 block中會(huì)將解析的數(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文件中圖片的個(gè)數(shù) size_t count = CGImageSourceGetCount(source); //定義一個(gè)變量記錄gif播放一輪的時(shí)間 float allTime=0; //存放所有圖片 NSMutableArray * imageArray = [[NSMutableArray alloc]init]; //存放每一幀播放的時(shí)間 NSMutableArray * timeArray = [[NSMutableArray alloc]init]; //存放每張圖片的寬度 (一般在一個(gè)gif文件中,所有圖片尺寸都會(huì)一樣) 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]];  CFRelease(info); } CFRelease(source); dataBlock(imageArray,timeArray,allTime,widthArray,heightArray);}

為UIImageView添加一個(gè)設(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) {   //添加幀動(dòng)畫   CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];   NSMutableArray * times = [[NSMutableArray alloc]init];   float currentTime = 0;   //設(shè)置每一幀的時(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è)置播放總時(shí)長(zhǎng)   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動(dòng)態(tài)圖數(shù)據(jù)

    iOS中的UIWebView功能十分強(qiáng)大,可以通過UIWebView為載體,來展示gif圖。并且這種方法也十分簡(jiǎn)單,代碼如下:

 //讀取gif數(shù)據(jù)   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];  //設(shè)置縮放模式  webView.scalesPageToFit = YES;  //用webView加載數(shù)據(jù)  [webView loadData:gifData MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];

四、兩種加載gif動(dòng)態(tài)圖方式的優(yōu)劣

    經(jīng)過測(cè)試,從加載速度上來說,通過UIImageView類別加載的方式更加快速,UIWebView的方式加載時(shí)間會(huì)稍長(zhǎng),但是從性能上來比較,WebView的方式性能更優(yōu),播放的gif動(dòng)態(tài)圖更加流暢。在開發(fā)中,可以根據(jù)需求,適當(dāng)選擇,例如雖然WebView加載的方式性能更好,但是在許多情況下,原生的UIImageView能夠更加自由的讓開發(fā)者進(jìn)行擴(kuò)展。

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 枣阳市| 清丰县| 漯河市| 吉安市| 新乐市| 栖霞市| 洛南县| 油尖旺区| 甘孜县| 梁河县| 沅陵县| 清水县| 连平县| 玉树县| 北辰区| 绥化市| 盐山县| 萝北县| 阿勒泰市| 平湖市| 城步| 大庆市| 修武县| 突泉县| 铜梁县| 东山县| 项城市| 叙永县| 体育| 大冶市| 桐乡市| 化隆| 黔南| 三门峡市| 安福县| 宣汉县| 西乌| 上高县| 横峰县| 龙井市| 新丰县|