一年又见底了,一年收集的资料文档,趁现在的空闲会陆续的整理出来。。。
<一、GIF的动画播放>
实现动画Gif的播放需要引入
#import <ImageIO/ImageIO.h>
#import <MobileCoreServices/MobileCoreServices.h>
这些头文件,以下是代码实现
@interface GifLoadingView()
@property (nonatomic, assign) size_t index; // 当前的帧数
@property (nonatomic, assign) size_t count; // 一共多少帧
@property (nonatomic, strong) NSTimer *gifTimer; // 定时器
@property (nonatomic, assign) CGImageSourceRef gifSource; // 图片资源
@property (nonatomic, strong) NSDictionary *gifDic; // gif动画属性
// 开始动画
- (void)beginAnimation;
@end
@implementation GifLoadingView
- (void)createGif
{
NSDictionary *gifLoopCount = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount];
self.gifDic = [NSDictionary dictionaryWithObject:gifLoopCount forKey:(NSString *)kCGImagePropertyGIFDictionary];
// 加载本地资源
NSData *gif = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"loading" ofType:@"gif"]];
self.gifSource = CGImageSourceCreateWithData((CFDataRef)self.gifSource, (CFDictionaryRef)self.gifDic);
self.count = CGImageSourceGetCount(self.gifSource);
// 获取gif的第一张图片
CGImageRef imageRef = CGImageSourceCreateImageAtIndex(self.gifSource, self.index, (CFDictionaryRef)self.gifDic);
self.layer.contents = (__bridge id)imageRef;
// 使用完后需要手动释放
CFRelease(imageRef);
}
- (void)beginAnimation
{
self.gifTimer = [NSTimer timerWithTimeInterval:0.02 target:self selector:@selector(startLoading) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:self.gifTimer forMode:NSDefaultRunLoopMode];
}
- (void)startLoading
{
self.index ++;
self.index = self.index % self.count;
if (self.index % self.count == 0) {
[self stopGif];
return;
}
CGImageRef ref = CGImageSourceCreateImageAtIndex(self.gif, self.index, (CFDictionaryRef)self.gifDic);
self.layer.contents = (__bridge id)ref;
CFRelease(ref);
}
// 取消定时器
- (void)stopGif
{
[self.gifTimer invalidate];
self.gifTimer = nil;
}
- (void)dealloc
{
CFRelease(self.gifSource);
}
@end
最后添加一张关于ImageIO的全家福
参考资料:http://www.haogongju.net/art/1328578