您的位置:首页 > 移动开发 > IOS开发

cocoa ios iphone xcode 播放GIF动画

2012-12-06 10:24 323 查看
使用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]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: