使用 UIWebView 播放
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
if let path = Bundle.main.path(forResource: "IMG_2073", ofType: "gif") {
if let gifData = NSData(contentsOfFile: path) {
//UIWebView生成
let rect = CGRect(x: 0, y: 302, width: 400, height: 400)
let imageWebView = UIWebView(frame: rect)
//用户不可交互
imageWebView.isUserInteractionEnabled = false
//加载gif数据
let baseUrl = URL(fileURLWithPath: "")
imageWebView.load(gifData as Data, mimeType: "image/gif", textEncodingName: "", baseURL: baseUrl)
//视图添加此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
#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;
}