iOS-使用系统类库加载gif格式图片
2014-11-05 20:13
423 查看
- (id)initWithFrame:(CGRect)frame filePath:(NSString *)_filePath{
self = [super
initWithFrame:frame];
if (self) {
/*
*_filePath : gif路径
*data : 取得这个gif
*/
NSData *data = [NSData
dataWithContentsOfFile:_filePath];
/*
*gifLoopCount : 设置一个gif的循环属性 ,值为0
*/
NSDictionary *gifLoopCount = [NSDictionary
dictionaryWithObjectsAndKeys:
[NSNumber
numberWithInt:0] , (NSString *)kCGImagePropertyGIFLoopCount,nil
];
/*
*创建gif属性
*/
NSDictionary * gifProperties = [NSDictionary
dictionaryWithObject:gifLoopCount
forKey:(NSString *)kCGImagePropertyGIFDictionary] ;
/*
*根据属性
还有data 得到gif,并存在CGImageSourceRef中
*{
* ColorModel = RGB;
* Depth = 8;//
* HasAlpha = 1;
* PixelHeight = 22;
* PixelWidth = 22;
* "{GIF}" = {
* DelayTime = "0.1";
* UnclampedDelayTime = "0.1";
* };
*}
*/
CGImageSourceRef gif =
CGImageSourceCreateWithData((__bridge
CFDataRef)(data), (__bridge
CFDictionaryRef)gifProperties);
CFDictionaryRef gifprops =(CGImageSourceCopyPropertiesAtIndex(gif,0,NULL));
/*
*count : gif的张数
*/
NSInteger count =CGImageSourceGetCount(gif);
CFDictionaryRef gifDic =
CFDictionaryGetValue(gifprops,
kCGImagePropertyGIFDictionary);
/*
*delay: 延迟时间??
*/
NSNumber * delay =
CFDictionaryGetValue(gifDic,
kCGImagePropertyGIFDelayTime);
/*
*unclampedDelay: 延迟时间??
*/
NSNumber * unclampedDelay =
CFDictionaryGetValue(gifDic,
kCGImagePropertyGIFUnclampedDelayTime);
//////注:本人不是做美术的,不懂gif原理,但经过我试验, DelayTime和UnclampedDelayTime
应该是取UnclampedDelayTime做出来的图才和用浏览器打开的时间相同
NSTimer *timer = [NSTimer
scheduledTimerWithTimeInterval:delay.floatValue
target:self
selector:@selector(play)
userInfo:nil
repeats:YES];
[timer
fire];
CFRelease(gifprops);
CFRelease(gif);
}
}
-(void)play
{
index ++;
index = index%count;
CGImageRef ref = CGImageSourceCreateImageAtIndex(gif, index, (CFDictionaryRef)gifProperties);
self.layer.contents = (id)ref;
CFRelease(ref);
}
-(void)removeFromSuperview
{
NSLog(@"removeFromSuperview");
[timer
invalidate];
timer =
nil;
[super
removeFromSuperview];
}
- (void)dealloc {
NSLog(@"dealloc");
CFRelease(gif);
[gifProperties release];
[super
dealloc];
}
self = [super
initWithFrame:frame];
if (self) {
/*
*_filePath : gif路径
*data : 取得这个gif
*/
NSData *data = [NSData
dataWithContentsOfFile:_filePath];
/*
*gifLoopCount : 设置一个gif的循环属性 ,值为0
*/
NSDictionary *gifLoopCount = [NSDictionary
dictionaryWithObjectsAndKeys:
[NSNumber
numberWithInt:0] , (NSString *)kCGImagePropertyGIFLoopCount,nil
];
/*
*创建gif属性
*/
NSDictionary * gifProperties = [NSDictionary
dictionaryWithObject:gifLoopCount
forKey:(NSString *)kCGImagePropertyGIFDictionary] ;
/*
*根据属性
还有data 得到gif,并存在CGImageSourceRef中
*{
* ColorModel = RGB;
* Depth = 8;//
* HasAlpha = 1;
* PixelHeight = 22;
* PixelWidth = 22;
* "{GIF}" = {
* DelayTime = "0.1";
* UnclampedDelayTime = "0.1";
* };
*}
*/
CGImageSourceRef gif =
CGImageSourceCreateWithData((__bridge
CFDataRef)(data), (__bridge
CFDictionaryRef)gifProperties);
CFDictionaryRef gifprops =(CGImageSourceCopyPropertiesAtIndex(gif,0,NULL));
/*
*count : gif的张数
*/
NSInteger count =CGImageSourceGetCount(gif);
CFDictionaryRef gifDic =
CFDictionaryGetValue(gifprops,
kCGImagePropertyGIFDictionary);
/*
*delay: 延迟时间??
*/
NSNumber * delay =
CFDictionaryGetValue(gifDic,
kCGImagePropertyGIFDelayTime);
/*
*unclampedDelay: 延迟时间??
*/
NSNumber * unclampedDelay =
CFDictionaryGetValue(gifDic,
kCGImagePropertyGIFUnclampedDelayTime);
//////注:本人不是做美术的,不懂gif原理,但经过我试验, DelayTime和UnclampedDelayTime
应该是取UnclampedDelayTime做出来的图才和用浏览器打开的时间相同
NSTimer *timer = [NSTimer
scheduledTimerWithTimeInterval:delay.floatValue
target:self
selector:@selector(play)
userInfo:nil
repeats:YES];
[timer
fire];
CFRelease(gifprops);
CFRelease(gif);
}
}
-(void)play
{
index ++;
index = index%count;
CGImageRef ref = CGImageSourceCreateImageAtIndex(gif, index, (CFDictionaryRef)gifProperties);
self.layer.contents = (id)ref;
CFRelease(ref);
}
-(void)removeFromSuperview
{
NSLog(@"removeFromSuperview");
[timer
invalidate];
timer =
nil;
[super
removeFromSuperview];
}
- (void)dealloc {
NSLog(@"dealloc");
CFRelease(gif);
[gifProperties release];
[super
dealloc];
}
相关文章推荐
- ios-使用SDWebImage加载Gif图片的问题
- c++使用std_image.c来加载jpg,png,gif等格式图片
- iOS开发之SDWebImage(一个可管理远程图片加载的类库)的使用
- iOS 使用SDWebImage加载gif链接图片时内存过大问题
- IOS开发中图片加载类库的使用 EGOImageLoader,SDWebImage
- iOS开发 - SDWebImage使用(一个可管理远程图片加载的类库)
- Xamarin.ios 使用webview加载gif图片
- VC中加载GIF格式动态图片的详细方法!
- 用API OleLoadPicture通过IStream来加载JPG、GIF格式的图片
- VC中使用GIF格式图片美化界面
- ios 图片加载方式,内存控制,尽量避免使用imageNamed,使用ARC也需要
- 用API OleLoadPicture通过IStream来加载JPG、GIF格式的图片
- 用API OleLoadPicture通过IStream来加载JPG、GIF格式的图片
- 用API OleLoadPicture通过IStream来加载JPG、GIF格式的图片
- IOS异步图片加载SDWebImage的使用时遇到file not found和Symbol Not Found: _objc_storeStrong
- VC中使用GIF格式图片美化界面
- SDWebImage使用——一个可管理远程图片加载的类库
- VC中加载GIF格式动态图片的详细方法!
- IOS_1,ios多线程使用:NSThread和NSOperation异步加载图片
- 用API OleLoadPicture通过IStream来加载JPG、GIF格式的图片