iOS GIF 格式动画 图片显示
2015-01-15 12:32
357 查看
在 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"];
完成!
如果中 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"];
完成!
相关文章推荐
- iOS GIF 格式动画 图片显示
- iOS GIF 格式动画 图片显示
- iOS GIF 格式动画 图片显示
- iOS:在界面显示gif动画图片
- ios显示gif格式图片
- iOS开发之旋转动画及GIF格式图片的播放
- iOS支持Gif格式图片动画
- delphi显示 jpg、png、gif 图片及 gif 动画的方法
- Delphi中的jpg、png、gif 图片及 gif 动画的显示
- Qt程序在运行的目标机上Gif等格式的图片无法显示问题
- 我的VC++——对话框中显示GIF格式的图片
- C++中使用CAtlHttpClient显示网络图片的方法,支持png,bmp,gif图片格式
- IOS使用UIImageView显示gif动画的例子
- 美化自己的对话框--MFC显示GIF动画图片
- Flex :自定义Loading载入状态(含图标) 及解决Flex中GIF动画图片加载时显示动起来
- 美化自己的对话框--MFC显示GIF动画图片
- IOS显示GIF图片
- Java之简单的图片动态显示(实现类似GIF动画效果)
- 显示 jpg、png、gif 图片及 gif 动画 - 回复 "格兰特" 的问题
- 显示GIF格式图片遇到的问题