iOS 播放本地 gif

一.使用 UIWebView 播放

    NSData *gifData = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"IMG_2073" ofType:@"GIF"]];
    //UIWebView生成
    UIWebView *imageWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 302, 400, 400)];
    //用户不可交互
    imageWebView.userInteractionEnabled = NO;
    //加载gif数据
    [imageWebView loadData:gifData MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
    //视图添加此gif控件
    [self.view addSubview:imageWebView];

二.使用 UIImageView + SDWebImage

//头文件
#import "UIImage+GIF.h"

-(void)gifPlay{
 UIImage      *image=[UIImage sd_animatedGIFNamed:@"IMG_2073"];
/*
注意 :SDWebImage 默认 动图格式为小写"gif"所以拖到项目中的 gif 文件记得拓展名要小写 否则动图不播放
*/

/*   通过二进制播放
NSString *path = [[NSBundle mainBundle] pathForResource:@"IMG_2073" ofType:@"gif"];
NSData *data = [NSData dataWithContentsOfFile:path];
UIImage  *image=[UIImage sd_animatedGIFWithData:data];
*/

 UIImageView  *gifview=[[UIImageView alloc]initWithFrame:CGRectMake(50,50,image.size.width, image.size.height)];
 gifview.image=image;
 [self.view addSubview:gifview];
 }

三. 自定义 MBProgressHUD
UIImageView + SDWebImage + MBProgressHUD

#import "UIImage+GIF.h"
#import "MBProgressHUD.h" 
-(void)showHUD {
    UIImage *image=[UIImage sd_animatedGIFNamed:@"IMG_2073"];
    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.backgroundColor = [UIColor redColor];
    //文字颜色
    hud.contentColor = [UIColor blueColor];
    //内容背景颜色
    hud.bezelView.backgroundColor =  [UIColor orangeColor];
    hud.mode = MBProgressHUDModeCustomView;
    hud.label.text = @"加载中....";
    hud.customView=gifview;
}

你可能感兴趣的:(iOS 播放本地 gif)