iOS GIF 格式动画 图片显示
来源:互联网 发布:网络嗅探器sn 编辑:程序博客网 时间:2024/05/16 22:25
在 iOS 开发中,我们常使用 SDK 中的 UIImageView 显示图片,使用 UIImage 解码图片文件或数据,其支持 PNG, JEPG, BMP, GIF 等格式。其中 GIF 图片不支持动画。
如果中 iOS 中显示 GIF 动画图片,则需要自己实现。具体实现方式很简单,使用 SDK 中的 Image I/O framework 解码图片,读取 GIF 动画图片的帧数,显示时间。
然后使用 SDK 中的 quartzcore framework 中的 core animation 的 CAKeyFrameAnimation 类对 CALayer 执行动画。
1. 解码图片,在 iOS Create an CGImageSource 文章已经介绍了如何使用 Image I/O framework 进行图片解码。
2. 读取 GIF 动画图片的每一帧图片及显示时间,并保存成数组:
size_t imageCount = CGImageSourceGetCount(cImageSource); NSMutableArray *images = [[NSMutableArray alloc] initWithCapacity:imageCount]; NSMutableArray *times = [[NSMutableArray alloc] initWithCapacity:imageCount]; NSMutableArray *keyTimes = [[NSMutableArray alloc] initWithCapacity:imageCount]; float totalTime = 0; for (size_t i = 0; i < imageCount; i++) { CGImageRef cgimage= CGImageSourceCreateImageAtIndex(cImageSource, i, NULL); [images addObject:(__bridge id)cgimage]; CGImageRelease(cgimage); NSDictionary *properties = (__bridge NSDictionary *)CGImageSourceCopyPropertiesAtIndex(cImageSource, i, NULL); NSDictionary *gifProperties = [properties valueForKey:(__bridge NSString *)kCGImagePropertyGIFDictionary]; NSString *gifDelayTime = [gifProperties valueForKey:(__bridge NSString* )kCGImagePropertyGIFDelayTime]; [times addObject:gifDelayTime]; totalTime += [gifDelayTime floatValue]; _size.width = [[properties valueForKey:(NSString*)kCGImagePropertyPixelWidth] floatValue]; _size.height = [[properties valueForKey:(NSString*)kCGImagePropertyPixelHeight] floatValue]; } float currentTime = 0; for (size_t i = 0; i < times.count; i++) { float keyTime = currentTime / totalTime; [keyTimes addObject:[NSNumber numberWithFloat:keyTime]]; currentTime += [[times objectAtIndex:i] floatValue]; }
3. 执行 CAKeyFrameAnimation 动画
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"]; [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]]; [animation setValues:images]; [animation setKeyTimes:keyTimes]; animation.duration = totalTime; animation.repeatCount = HUGE_VALF; [self.layer addAnimation:animation forKey:@"gifAnimation"];
完成!
0 0
- iOS GIF 格式动画 图片显示
- iOS GIF 格式动画 图片显示
- iOS GIF 格式动画 图片显示
- ios显示gif格式图片
- iOS支持Gif格式图片动画
- IOS显示GIF图片
- Android显示gif格式图片
- Android显示gif格式图片
- iOS中显示GIF动画
- 使用Qt显示GIF格式动画
- IOS gif图片的显示
- PictureBox控件中显示GIF动画(图片)
- iOS开发之旋转动画及GIF格式图片的播放
- android 显示gif格式的图片
- 自定义View来显示gif格式图片
- 显示 jpg、png、gif 图片及 gif 动画
- iOS 动画大全(附带gif图片效果)
- iOS 图片gif格式转化为数组
- lower_bound和upper_bound,原来都是求刚好大于的值
- 推广API 网址
- ActiveMQ的集群配置
- 91 阶乘之和
- leetcode 刷题之路 8 Longest Common Prefix
- iOS GIF 格式动画 图片显示
- Minimal coverage-区间覆盖问题
- 解决eclipse中egit中的cannot open git-upload-pack有关问题
- Android 中json 对象解析
- URAL 1353 Milliard Vasya's Function(DP)
- C# 计算时间段的方法-strFlag为自定义的字符串标识
- 93 汉诺塔(三)
- js把字符串转换成对象
- 解决keta-custom下载patchca失败的方法