NSDictionary *gifProperties; // 保存gif动画属性
size_t index; // gif动画播放开始的帧序号
size_t count; // gif动画的总帧数
NSTimer *timer; // 播放gif动画所使用的timer
initWithFrame:(NSData*)data{
gifProperties = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount]
forKey:(NSString *)kCGImagePropertyGIFDictionary];
gif = CGImageSourceCreateWithData((CFDataRef)data, (CFDictionaryRef)gifProperties);
count =CGImageSourceGetCount(gif);
timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(play) userInfo:nil repeats:YES];
}
播放
play{
index ++;
index = index%count;
CGImageRef ref = CGImageSourceCreateImageAtIndex(gif, index, (CFDictionaryRef)gifProperties);
self.layer.contents = (__bridge id)ref;
CFRelease(ref);
}