iOS - 多种Gif播放方式

** 1.系统UIImageView 多张图片组成动画(帧动画)**

-(void)MSUGifPlay  {  
    //    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"],     

** 2.利用第三方库**

 **1)SDWebImageView里的UIImage+GIF**

        -(void)MSUGifPlay  {    
            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];
      } 

**2)IImageView-PlayGIF  YFGIFImageView**

        /**   *  UIImageView-PlayGIF 是 UIImageView 子类,用来显示 GIF。UIIMageView-PlayGIF 性能高,而且占用的内存很低。   *  https://github.com/yfme/UIImageView-PlayGIF   *  Memory-21.9M   *  #import "YFGIFImageView.h"   */  
    -(void)MSUGifPlay  {     
         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];
      }    

 **3)SCGIFImageView**

        /**   *  摘要: SCGIFImageView是一个开源的GIF图片动画显示控件,通过将GIF的每一帧都取出来生成UIImage对象存放在一个数组中,然后使用NSTimer进行动画轮转。   *  https://github.com/shichangone/SCGifExample   *  Memory-22.5M   *  #import "SCGIFImageView.h"   */  
    -(void)MSUGifPlay  {   
       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]; 
     }    

 **4)YLGIFImage**

            /**   *  YLGIFImage 是异步 GIF 图像解码器和图像查看器,支持播放 GIF 图像,而且使用很少的内存。   *  https://github.com/liyong03/YLGIFImage   *  Memory-22.7M   *  #import "YLImageView.h"   *  #import "YLGIFImage.h"   */ 
     -(void)MSUGifPlay  { 
         YLImageView* imageView = [[YLImageView alloc] initWithFrame:CGRectMake(0, 160, 320, 240)];    
        [self.view addSubview:imageView];  
        imageView.image = [YLGIFImage imageNamed:@"test.gif"]; 
     }    

3. 为MBProgressHUD 添加加载动画

        /**   *  MBProgressHUD 添加加载动画   *  Memory-23.8M   *  #import "UIImage+GIF.h"   *  #import "MBProgressHUD.h"   */  
    -(void)MSUGifPlay  {    
          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; 
     }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 15,584评论 4 61
  • 这个世界很大, 直到这个年龄我们才相遇, 这个世界很小, 小到即使奔赴远方, 我们仍可相遇。 在月光的掩映下。 我...
    九九梦阅读 193评论 0 0
  • 静态变量与成员变量的区别: 静态变量是放在静态区,属于类的一部分 只有一份. 成员变量是放在堆区,属于对象,每...
    Miss_差不多阅读 163评论 0 0

友情链接更多精彩内容