gif 图片的展示方式有几种:webView方法、逐桢展示、第三方
webView展示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| CGRect frame = CGRectZero;
frame.size = [UIImage imageNamed:@"x.jpg"].size;
NSData gif = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"x" ofType:@"jpg"]]; UIWebView *webView = [[UIWebView alloc] initWithFrame:frame];
webView.userInteractionEnabled = NO;
[webView loadData:gif MIMEType:@"image/jpg" textEncodingName:nil baseURL:nil];
[self.view addSubview:webView];
|
逐桢展示(animation效果)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| UIImageView *gifView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 0, self.view.frame.size.width - 40,300)]; NSMutableArray *gifArray = [[NSMutableArray alloc] initWithCapacity:20]; for (NSInteger i = 0; i < 20; i++) { UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld.jpg",(long)i]]; [gifArray addObject:image]; } gifView.image = gifArray.lastObject;
gifView.animationImages = gifArray;
gifView.animationDuration = 10;
gifView.animationRepeatCount = 0; [gifView startAnimating];
[self.view addSubview:gifView];
|
第三方
貌似有不少第三方,这个OLImage这个用起来还是挺简单的,而且图片的大小是铺满的填充效果。gitHub地址:https://github.com/dtorres/OLImageView
在当前controller或view中导入OLImageView.h和OLImage.h两个头文件即可
1 2 3 4 5 6
| self.gifView = [[OLImageView alloc] initWithFrame:CGRectMake(0, 100, 300, 300)]; self.gifView.center = self.view.center; NSString *imgPath = [[NSBundle mainBundle] pathForResource:@"x" ofType:@"jpg"];/*gif图路径*/ /*NSData转为UIImage*/ self.gifView.image = [OLImage imageWithData:data]; [self.view addSubview:self.gifView];
|