一年又见底了,一年收集的资料文档,趁如今的空闲会陆续的整理出来。。。oop
<1、GIF的动画播放>动画
实现动画Gif的播放须要引入atom
#import <ImageIO/ImageIO.h>
#import <MobileCoreServices/MobileCoreServices.h>
这些头文件,如下是代码实现
spa
@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动画属性.net
// 开始动画blog
- (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);get
// 获取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