本次實現gif動畫播放是通過將動畫文件讀取到CGImageSourceRef,然后用NSTimer來播放的。
代碼如下:
首先是頭文件
#import <UIKit/UIKit.h>
#import <ImageIO/ImageIO.h>
#import <MobileCoreServices/MobileCoreServices.h>
@interface GifView : UIView
{
CGImageSourceRef gif; // 保存gif動畫
NSDictionary *gifProperties; // 保存gif動畫屬性
size_t index; // gif動畫播放開始的幀序號
size_t count; // gif動畫的總幀數
NSTimer *timer; // 播放gif動畫所使用的timer
}
- (id)initWithFrame:(CGRect)frame filePath:(NSString *)filePath;
- (void)stopGif;
@end
接下來是實現
#import "GifView.h"
#import <QuartzCore/QuartzCore.h>
@implementation GifView
- (id)initWithFrame:(CGRect)frame filePath:(NSString *)filePath
{
self = [super initWithFrame:frame];
if (self)
{
NSDictionary *gifLoopCount = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount];
gifProperties = [[NSDictionary dictionaryWithObject:gifLoopCount forKey:(NSString *)kCGImagePropertyGIFDictionary] retain];
gif = CGImageSourceCreateWithURL((CFURLRef)[NSURL fileURLWithPath:path], (CFDictionaryRef)gifProperties);
count =CGImageSourceGetCount(gif);
timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(play) userInfo:nil repeats:YES];
[timer fire];
}
return self;
}
-(void)play
{
index ++;
index = index%count;
CGImageRef ref = CGImageSourceCreateImageAtIndex(gif, index, (CFDictionaryRef)gifProperties);
self.layer.contents = (id)ref;
CFRelease(ref);
}
- (void)dealloc
{
NSLog(@"dealloc");
CFRelease(gif);
[gifProperties release];
[super dealloc];
}
- (void)stopGif
{
[timer invalidate];
timer = nil;
}
@end
這個類比較簡單,在方法
- (id)initWithFrame:(CGRect)frame filePath:(NSString *)filePath
調用結束就開始播放動畫,如果需要用戶指定何時播放的話,只需要把timer的開始放到合適的位置。通過對CFDictonaryRaf 也就是gifProperties的改變,我們還可以控制動畫是否循環播放以及循環多少次停止。
通過對index的改變也可以控制動畫從某幀開始播放。同理,同時改變index和count的話,也可以控制從某幀到某幀的播放。
注:必須調用方法
- (void)stopGif
之后才可以退出這個類。否則timer不會關閉,產生內存泄露。
另注:需要QuartzCore.framework;MobileCoreServices.framework;ImageIO.framework 這三個framework的支持