cocoa ios iphone xcode 播放GIF动画
使用imageio的这个自带的framework, 这个库也是apple的webkit所使用的,可以参考apple的opensource的webkit实现。 因此,这个 库从性能和蒹容性方面应该都是最佳选择
以下是代码,比较简单
NSDictionary *gifLoopCount = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:0] , (NSString *)kCGImagePropertyGIFLoopCount,nil
];
NSDictionary * gifProperties = [NSDictionary dictionaryWithObject:gifLoopCount forKey:(NSString *)kCGImagePropertyGIFDictionary] ;
CGImageSourceRef gif = CGImageSourceCreateWithData((__bridge CFDataRef)(data), (__bridge CFDictionaryRef)gifProperties);
CFDictionaryRef gifprops =(CGImageSourceCopyPropertiesAtIndex(gif,0,NULL));
NSInteger count =CGImageSourceGetCount(gif);
CFDictionaryRef gifDic = CFDictionaryGetValue(gifprops, kCGImagePropertyGIFDictionary);//[gifprops objectForKey:(NSString *)kCGImagePropertyGIFDictionary];
NSNumber * delay = CFDictionaryGetValue(gifDic, kCGImagePropertyGIFDelayTime); //[gifDic objectForKey:(NSString *)kCGImagePropertyGIFDelayTime];
NSNumber * w = CFDictionaryGetValue(gifprops, @"PixelWidth");
NSNumber * h =CFDictionaryGetValue(gifprops, @"PixelHeight");
totalDuration = delay.doubleValue * count;
pixelWidth = w.intValue;
pixelHeight = h.intValue;
images = [[NSMutableArray alloc] init];
for(int index=0;index<count;index++)
{
CGImageRef ref = CGImageSourceCreateImageAtIndex(gif, index, nil);
UIImage *img = [UIImage imageWithCGImage:ref];
[images addObject:img];
CFRelease(ref);
}
CFRelease(gifprops);
CFRelease(gif);
解压完之后,直接用IMageview就可以播放了。代码如下。
UIImageView *image = ....;
image.animationDuration = totalDuration;
image.animationImages = images;
[image startAnimating]
相关新闻>>
- 发表评论
-
- 最新评论 更多>>
今日头条
更多>>您可能感兴趣的文章
- (iPhone/iPad开发)presentViewController相应方法在SDK5.0前后
- ios学习笔记(二)xcode 4.3.2下实现基本交互
- Android:实现TabWidget选项卡按钮在屏幕下方
- Lexical or Preprocessor Issue 'xxx.h
- iPhone开发学习笔记006—— NSNotification自定义通知名
- iphone开发之object c基础一定义接口
- 多线程之NSInvocationOperation
- Memory on the iPhone
- iPhone应用开发之十一:常用的三种动画表现效果
- ios开发问题:添加库和复制其它工程文件编译错误问题