简单记录一下,排除第三方加载gif
1. 使用webview ,可以根据需要将webview放到一些控件上
如下:
-(void)loadGif{
//1.找到gif文件路径 ,SDKPlatform.bundle/loading1 是所在SDKPlatform.bundle中
NSString *dataPath = [[NSBundle mainBundle]pathForResource:@"SDKPlatform.bundle/loading1" ofType:@"gif"];
//转化成data
NSData *gif = [NSData dataWithContentsOfFile:dataPath];
UIWebView *webViewBG = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0,WDScale(250), WDScale(160))];
//加载data的形式
[webViewBG loadData:gif MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
[webViewBG setScalesPageToFit:YES];//自动适应当前frame大小
[self.view addSubview:webViewBG];
}
或者如下
-(void)loadGif{
UIWebView *webViewBG = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0,WDScale(250), WDScale(160))];
//根据路径转成NSURL
NSURL *url = [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"SDKPlatform.bundle/loading1" ofType:@"gif"]];
[webViewBG loadRequest:[NSURLRequest requestWithURL:url]];
[webViewBG setScalesPageToFit:YES];//自动适应当前frame大小
[self.view addSubview:webViewBG];
}
2.使用拆分gif图 ,imageView来动画加载图片
- (void)createGIF {
//1.找到gif文件路径
NSString *dataPath = [[NSBundle mainBundle]pathForResource:@"SDKPlatform.bundle/loading1" ofType:@"gif"];
//2.获取gif文件数据
CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)[NSURL fileURLWithPath:dataPath], NULL);
//3.获取gif文件中图片的个数
size_t count = CGImageSourceGetCount(source);
//5.定义一个可变数组存放所有图片
NSMutableArray *imageArray = [[NSMutableArray alloc] init];
//遍历gif
for (size_t i=0; i<count; i++) {
CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL);
UIImage* imageName=[UIImage imageWithCGImage:image];
[imageArray addObject:imageName];//存image
CGImageRelease(image);
}
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,WDScale(250), WDScale(160))];
[self.view addSubview:imageView];
imageView.animationImages=imageArray;//将图片数组加入UIImageView动画数组中
imageView.animationDuration=2;//每次动画时长
[imageView startAnimating];
}