1. 程式人生 > >iOS載入Gif圖片的N種方式

iOS載入Gif圖片的N種方式

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

/**   *   UIImageView 動畫   *   Memory-23M   */  -(void)gifPlay1  {  //    NSArray  *[email protected][@"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 
[email protected]
[[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];  }  

 

 

為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;  }  

 

其它

微部落格戶端 VVebo 的作者開源了他自己為VVebo寫的GIF解決方案 VVeboImageView, 佔用記憶體很小。(iOS移動開發週報-第4期)

 

 \

  Demo下載 http://yunpan.cn/cj6JBN7mfETWE (提取碼:226f)