V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
brance
V2EX  ›  iOS

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

  •  
  •   brance · 2017-04-19 12:03:17 +08:00 · 3895 次点击
    这是一个创建于 2556 天前的主题,其中的信息可能已经有所发展或是发生改变。

    以下是 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 友们给点思路。

    11 条回复    2017-04-20 09:00:12 +08:00
    andyzhshg
        1
    andyzhshg  
       2017-04-19 12:39:50 +08:00
    印象里 gif 是用的 256 色调色板来存储颜色的,所以每帧的画面至多有 256 种颜色,把真彩色的图片用 256 色表示,失真完全正常。
    brance
        2
    brance  
    OP
       2017-04-19 12:43:36 +08:00 via iPhone
    @andyzhshg 这个问题我也查阅了资料, 2 的八次方也就是 256 种颜色。超过了就出现了如述问题。
    andyzhshg
        3
    andyzhshg  
       2017-04-19 12:47:34 +08:00   ❤️ 1
    @brance 好像是可以每帧用不同的调色板,这样的话针对每帧的颜色分布提取最佳的调色板,理论上是可以增强一些效果的,不过对于每帧色彩都比较丰富的图像,也是杯水车薪了。
    deepjia
        4
    deepjia  
       2017-04-19 12:49:13 +08:00   ❤️ 1
    GIF 色彩少是常识
    标题真是(不说脏话)
    robinray
        5
    robinray  
       2017-04-19 13:21:44 +08:00
    楼主可以用 APNG 噢~~
    brance
        6
    brance  
    OP
       2017-04-19 13:47:39 +08:00
    @andyzhshg 我试试。
    bikkgry
        7
    bikkgry  
       2017-04-19 13:59:28 +08:00 via iPhone
    ....震惊部过来的吗
    zhangsimon
        8
    zhangsimon  
       2017-04-19 14:09:44 +08:00
    如果只考虑应用内展示的话,强烈要求视频替换 gif
    不仅色彩更丰富,同样的色彩采样和帧数,体积也更小
    brance
        9
    brance  
    OP
       2017-04-19 14:45:01 +08:00
    @zhangsimon 有这个需求没办法,如果
    1.我先把图片转成视频,再把视频转成 gif
    2. 图片直接转 gif
    不知道这种法子可行否,还是这两种的结果其实都是一样。
    DT27
        10
    DT27  
       2017-04-20 08:35:44 +08:00
    。。。。。。
    brance
        11
    brance  
    OP
       2017-04-20 09:00:12 +08:00 via iPhone
    @DT27 哈哈哈哈哈哈哈哈哈 嗝
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3259 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 12:57 · PVG 20:57 · LAX 05:57 · JFK 08:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.