震惊! iOS 多张图片合成 GIF 后某些照片竟然会变得有点模糊,甚至有些丰富色彩的图片还会大片变白!

2017-04-19 12:03:17 +08:00
 brance

以下是 OC 代码 NSArray *document = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);

NSString *documenStr = [document objectAtIndex:0];

NSFileManager *fileManager = [NSFileManager defaultManager];

NSString *textDic= [documenStr  stringByAppendingString:@"/gif"];

[fileManager createDirectoryAtPath:textDic withIntermediateDirectories:YES attributes:nil error:nil];

NSString *path = [textDic stringByAppendingString:@"zomake.gif"];
NSLog(@"path = %@",path);

CGImageDestinationRef destion;

CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)path, kCFURLPOSIXPathStyle, false);

destion = CGImageDestinationCreateWithURL(url, kUTTypeGIF, imagePathArray.count, NULL);

NSDictionary *frameDic = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:0.3] forKey:(NSString *)kCGImagePropertyGIFDelayTime]
                                                     forKey:(NSString *)kCGImagePropertyGIFDictionary];
                                                     
NSMutableDictionary *gifParmdict = [NSMutableDictionary dictionaryWithCapacity:2];

[gifParmdict setObject:[NSNumber numberWithBool:YES] forKey:(NSString *)kCGImagePropertyGIFHasGlobalColorMap];

[gifParmdict setObject:(NSString *)kCGImagePropertyColorModelRGB forKey:(NSString *)kCGImagePropertyColorModel];

[gifParmdict setObject:[NSNumber numberWithInt:8] forKey:(NSString *)kCGImagePropertyDepth];

[gifParmdict setObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount];

NSDictionary *gifProperty = [NSDictionary dictionaryWithObject:gifParmdict forKey:(NSString *)kCGImagePropertyGIFDictionary];

//单帧添加到 gif
for (UIImage *dimage in imagePathArray) {
    CGImageDestinationAddImage(destion, dimage.CGImage,(__bridge  CFDictionaryRef)frameDic);
}
CGImageDestinationSetProperties(destion, (__bridge  CFDictionaryRef)gifProperty);
CGImageDestinationFinalize(destion);
CFRelease(destion);

//此处解析 gif
NSData *data = [NSData dataWithContentsOfFile:path];

//此处将得到的 nsData 转为 gif 动画
UIImage *gifImage = [UIImage sd_tz_animatedGIFWithData:data];

望 V 友们给点思路。

3904 次点击
所在节点    iOS
11 条回复
andyzhshg
2017-04-19 12:39:50 +08:00
印象里 gif 是用的 256 色调色板来存储颜色的,所以每帧的画面至多有 256 种颜色,把真彩色的图片用 256 色表示,失真完全正常。
brance
2017-04-19 12:43:36 +08:00
@andyzhshg 这个问题我也查阅了资料, 2 的八次方也就是 256 种颜色。超过了就出现了如述问题。
andyzhshg
2017-04-19 12:47:34 +08:00
@brance 好像是可以每帧用不同的调色板,这样的话针对每帧的颜色分布提取最佳的调色板,理论上是可以增强一些效果的,不过对于每帧色彩都比较丰富的图像,也是杯水车薪了。
deepjia
2017-04-19 12:49:13 +08:00
GIF 色彩少是常识
标题真是(不说脏话)
robinray
2017-04-19 13:21:44 +08:00
楼主可以用 APNG 噢~~
brance
2017-04-19 13:47:39 +08:00
@andyzhshg 我试试。
bikkgry
2017-04-19 13:59:28 +08:00
....震惊部过来的吗
zhangsimon
2017-04-19 14:09:44 +08:00
如果只考虑应用内展示的话,强烈要求视频替换 gif
不仅色彩更丰富,同样的色彩采样和帧数,体积也更小
brance
2017-04-19 14:45:01 +08:00
@zhangsimon 有这个需求没办法,如果
1.我先把图片转成视频,再把视频转成 gif
2. 图片直接转 gif
不知道这种法子可行否,还是这两种的结果其实都是一样。
DT27
2017-04-20 08:35:44 +08:00
。。。。。。
brance
2017-04-20 09:00:12 +08:00
@DT27 哈哈哈哈哈哈哈哈哈 嗝

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/355846

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX