准备:本地图片资源,GifView
GifView代码:
/**%20*%20%20调用结束就开始播放动画,如果需要用户指定何时播放的话,只需要把timer的开始放到合适的位置。通过对CFDictonaryRaf%20也就是gifPRoperties的改变,我们还可以控制动画是否循环播放以及循环多少次停止。%20%20%20%20%20通过对index的改变也可以控制动画从某帧开始播放。同理,同时改变index和count的话,也可以控制从某帧到某帧的播放。%20%20%20%20注意:-%20(void)stopGif;之后才可以退出这个类。否则timer不会关闭,产生内存泄露。%20*/#import%20<UIKit/UIKit.h>#import%20<ImageIO/ImageIO.h>@interface%20GifView%20:%20UIView%20{%20%20%20%20CGImageSourceRef%20gif;%20//%20保存gif动画%20%20%20%20NSDictionary%20*gifProperties;%20%20//%20保存gif动画属性%20%20%20%20size_t%20index;//%20gif动画播放开始的帧序号%20%20%20%20size_t%20count;//%20gif动画的总帧数%20%20%20%20NSTimer%20*timer;//%20播放gif动画所使用的timer}-%20(id)initWithFrame:(CGRect)frame%20filePath:(NSString%20*)_filePath;-%20(id)initWithFrame:(CGRect)frame%20data:(NSData%20*)_data;-%20(void)stopGif;
#import%20"GifView.h"#import%20<QuartzCore/QuartzCore.h>@implementation%20GifView-%20(id)initWithFrame:(CGRect)frame%20filePath:(NSString%20*)_filePath{%20%20%20%20%20%20%20%20self%20=%20[super%20initWithFrame:frame];%20%20%20%20if%20(self)%20{%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20gifProperties%20=%20[NSDictionary%20dictionaryWithObject:[NSDictionary%20dictionaryWithObject:[NSNumber%20numberWithInt:0]%20forKey:(NSString%20*)kCGImagePropertyGIFLoopCount]%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20forKey:(NSString%20*)kCGImagePropertyGIFDictionary];%20%20%20%20%20%20%20%20gif%20=%20CGImageSourceCreateWithURL((CFURLRef)[NSURL%20fileURLWithPath:_filePath],%20(CFDictionaryRef)gifProperties);%20%20%20%20%20%20%20%20count%20=CGImageSourceGetCount(gif);%20%20%20%20%20%20%20%20timer%20=%20[NSTimer%20scheduledTimerWithTimeInterval:0.12%20target:self%20selector:@selector(play)%20userInfo:nil%20repeats:YES];%20%20%20%20%20%20%20%20[timer%20fire];%20%20%20%20}%20%20%20%20return%20self;}-%20(id)initWithFrame:(CGRect)frame%20data:(NSData%20*)_data{%20%20%20%20%20%20%20%20self%20=%20[super%20initWithFrame:frame];%20%20%20%20if%20(self)%20{%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20gifProperties%20=%20[NSDictionary%20dictionaryWithObject:[NSDictionary%20dictionaryWithObject:[NSNumber%20numberWithInt:0]%20forKey:(NSString%20*)kCGImagePropertyGIFLoopCount]%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20forKey:(NSString%20*)kCGImagePropertyGIFDictionary];%20%20%20%20%20%20%20%20//%20%20%20%20%20%20%20%20gif%20=%20CGImageSourceCreateWithURL((CFURLRef)[NSURL%20fileURLWithPath:_filePath],%20(CFDictionaryRef)gifProperties);%20%20%20%20%20%20%20%20gif%20=%20CGImageSourceCreateWithData((CFDataRef)_data,%20(CFDictionaryRef)gifProperties);%20%20%20%20%20%20%20%20count%20=CGImageSourceGetCount(gif);%20%20%20%20%20%20%20%20timer%20=%20[NSTimer%20scheduledTimerWithTimeInterval:0.12%20target:self%20selector:@selector(play)%20userInfo:nil%20repeats:YES];%20%20%20%20%20%20%20%20[timer%20fire];%20%20%20%20}%20%20%20%20return%20self;}-(void)play{%20%20%20%20index%20++;%20%20%20%20index%20=%20index%count;%20%20%20%20CGImageRef%20ref%20=%20CGImageSourceCreateImageAtIndex(gif,%20index,%20(CFDictionaryRef)gifProperties);%20%20%20%20self.layer.contents%20=%20(__bridge%20id)ref;%20%20%20%20CFRelease(ref);}-(void)removeFromSuperview{%20%20%20%20NSLog(@"removeFromSuperview");%20%20%20%20[timer%20invalidate];%20%20%20%20timer%20=%20nil;%20%20%20%20[super%20removeFromSuperview];}-%20(void)dealloc%20{%20%20%20%20NSLog(@"dealloc");%20%20%20%20CFRelease(gif);}-%20(void)stopGif{%20%20%20%20[timer%20invalidate];%20%20%20%20timer%20=%20nil;}
加载Gif的三种方式:(从网络或者本地)
-%20(NSData%20*)loadDataForIndex:(NSInteger)index%20{%20%20%20%20NSData%20*data%20=%20nil;%20%20%20%20if%20(index%20==%200)%20{%20%20%20%20%20%20%20%20//网络%20%20%20%20%20%20%20%20data%20=%20[NSData%20dataWithContentsOfURL:[NSURL%20URLWithString:@"http://s14.sinaimg.cn/mw690/005APVsyzy6MFOsVFfv5d&690"]];%20%20%20%20}else%20{%20%20%20%20%20%20%20%20//本地%20%20%20%20%20%20%20%20data%20=%20[NSData%20dataWithContentsOfFile:[[NSBundle%20mainBundle]%20pathForResource:@"run"%20ofType:@"gif"]];%20%20%20%20}%20%20%20%20return%20data;}
1.GifView
//第三方GifView(实现gif动画播放是通过将动画文件读取到CGImageSourceRef,然后用NSTimer来播放的。) //- (id)initWithFrame:(CGRect)frame filePath:(NSString *)_filePath; GifView *dataView = [[GifView alloc] initWithFrame:CGRectMake(0, 0, 100, 100) data:data]; [self.view addSubview:dataView];// [dataView stopGif];
2.webView(不会出现内存问题)
//webView UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 120, 100, 100)]; webView.backgroundColor = [UIColor redColor]; webView.scalesPageToFit = YES; [webView loadData:data MIMEType:@"image/gif" textEncodingName:nil baseURL:nil]; [self.view addSubview:webView];
3.帧动画
- (void)runGIFForImage { UIImageView *gifImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 240, 100, 100)]; NSArray *gifArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"1"], [UIImage imageNamed:@"2"], [UIImage imageNamed:@"3"], [UIImage imageNamed:@"4"], [UIImage imageNamed:@"5"], [UIImage imageNamed:@"6"], [UIImage imageNamed:@"7"], [UIImage imageNamed:@"8"], [UIImage imageNamed:@"9"], [UIImage imageNamed:@"10"], [UIImage imageNamed:@"11"], [UIImage imageNamed:@"12"], [UIImage imageNamed:@"13"], [UIImage imageNamed:@"14"], [UIImage imageNamed:@"15"], [UIImage imageNamed:@"16"], [UIImage imageNamed:@"17"], [UIImage imageNamed:@"18"], [UIImage imageNamed:@"19"], [UIImage imageNamed:@"20"], [UIImage imageNamed:@"21"], [UIImage imageNamed:@"22"],nil]; gifImageView.animationImages = gifArray; //动画图片数组 gifImageView.animationDuration = 5; //执行一次完整动画所需的时长 gifImageView.animationRepeatCount = 999; //动画重复次数 [gifImageView startAnimating]; [self.view addSubview:gifImageView];}
新闻热点
疑难解答