加载Gif图片方法
1、使用webView(直接加载已有gif图片)
NSString *filePath1 = [[NSBundle mainBundle]pathForResource:@"girl.gif" ofType:nil]; NSData *webGifData = [NSData dataWithContentsOfFile:filePath1]; UIWebView *webView = [[UIWebView alloc]init]; webView.frame = CGRectMake(100, 20, 100, 100); webView.backgroundColor = [UIColor blueColor]; webView.scalesPageToFit = YES; //最好设置一下userInteractionEnabled这个属性 webView.userInteractionEnabled = NO; [webView loadData:webGifData MIMEType:@"image/gif" textEncodingName:@"" baseURL:[NSURL URLWithString:@""]]; [self.view addSubview:webView];
2、使用分组图(需要UI切图,图片起名字最好规范一些!!!,其实就是逐帧动画,O(∩_∩)O哈哈哈~)
UIImageView *imagesView = [[UIImageView alloc]init]; imagesView.frame = CGRectMake(100, 140, 100, 100); NSMutableArray *array = [NSMutableArray new]; for (int i = 0; i<14; i++) { UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"loading_0000%d",i]]; [array addObject:image]; } imagesView.animationImages = array; imagesView.animationDuration = 0.5; imagesView.animationRepeatCount = MAXFLOAT;//这里也可以设置重复次数. [imagesView startAnimating]; [self.view addSubview: imagesView];
3、使用了SDwebImage第三方,导入"UIImage+GIF.h"头文件
NSString *Path = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:@"dog.gif" ofType:nil]; NSData *imageData = [NSData dataWithContentsOfFile:Path]; UIImageView *sdImage = [UIImageView new]; sdImage.image = [UIImage sd_animatedGIFWithData:imageData]; sdImage.frame = CGRectMake(100, 260, 150, 100); [self.view addSubview:sdImage];
4、从网络下载图片(这里使用的是SDwebimge方法)
NSString *urlString = @"http://c.hiphotos.baidu.com/zhidao/pic/item/3801213fb80e7becf636ec082c2eb9389a506b6f.jpg"; NSURL *url = [NSURL URLWithString:urlString]; NSData *data = [NSData dataWithContentsOfURL:url]; UIImageView *imageView = [[UIImageView alloc]init]; imageView.frame = CGRectMake(100, 380, 150, 100); imageView.image = [UIImage sd_animatedGIFWithData:data]; imageView.backgroundColor = [UIColor redColor]; [self.view addSubview:imageView];