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


原文轉自:http://www.2cto.com/kf/201603/491897.html



首頁 > 程序開發 > 移動開發 > IOS > 正文
iOS-Gif圖片展示N種方式(原生+第三方)
2016-03-03      0 個評論    來源:iOS愛好者  
收藏    我要投稿

原生方法:

1.UIWebView
特點:加載速度略長,性能更優,播放的gif動態圖更加流暢。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<code class="hljs objectivec">//動態展示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];
}</code>

2.UIImagView
加載的方式更加快速,性能不如UIWebView,優點:易於擴展

1)
增加一個UIImageView的類別(category),增加兩個方法
UIImage+Tool
.h

?
1
2
3
4
5
6
7
8
9
10
11
<code class="hljs java">#import <uikit uikit.h="">
 
@interface UIImageView (Tool)
 
/** 解析gif文件數據的方法 block中會將解析的數據傳遞出來 */
-(void)getGifImageWithUrk:(NSURL *)url returnData:(void(^)(NSArray<uiimage> * imageArray,NSArray<nsnumber>*timeArray,CGFloat totalTime, NSArray<nsnumber>* widths, NSArray<nsnumber>* heights))dataBlock;
 
/** 爲UIImageView添加一個設置gif圖內容的方法: */
-(void)yh_setImage:(NSURL *)imageUrl;
 
@end</nsnumber></nsnumber></nsnumber></uiimage></uikit></code>

.m

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<code class="hljs objectivec">//
//  UIImageView+Tool.m
//  OneHelper
//
//  Created by qiuxuewei on 16/3/2.
//  Copyright ? 2016年 邱學偉. All rights reserved.
//
 
#import "UIImageView+Tool.h"
//要引入ImageIO庫
#import <imageio imageio.h="">
 
@implementation UIImageView (Tool)
 
 
 
//解析gif文件數據的方法 block中會將解析的數據傳遞出來
-(void)getGifImageWithUrk:(NSURL *)url returnData:(void(^)(NSArray<uiimage> * imageArray, NSArray<nsnumber>*timeArray,CGFloat totalTime, NSArray<nsnumber>* widths,NSArray<nsnumber>* 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<count; __bridge="" __self="self;" __weak="" cgfloat="" cgimageref="" getgifimagewithurk:imageurl="" height="[[info" heightarray="" id="" image="CGImageSourceCreateImageAtIndex(source," imagearray="" info="(__bridge" nsdictionary="" nsnumber="" nsstring="" nsurl="" self="" time="[[timeDic" timearray="" timedic="[info" uiimage="" width="[[info" widtharray=""> *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;
        //設置每一幀的時間佔比
        for (int i=0; i<imagearray.count; __self="" addanimation:animation="" animation="" animation.duration="totalTime;" animation.repeatcount="MAXFLOAT;" camediatimingfunction="" code="" end="" nsnumber="" numberwithfloat:currenttime="" times="" uiimageview=""></imagearray.count;></nsnumber></nsnumber></nsnumber></count;></nsnumber></nsnumber></nsnumber></uiimage></imageio></code>

在加載gif的地方使用
導入 UIImageView+Tool

?
1
2
3
4
5
6
7
8
<code class="hljs objectivec"><code class="hljs objectivec">-(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];
 
}</code></code>

第三方:
1.YLGIFImage
github鏈接: https://github.com/liyong03/YLGIFImage


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章