【问题标题】:Xcode GIF output has color distorted when GIF has large amount of frames当 GIF 有大量帧时,Xcode GIF 输出颜色失真
【发布时间】:2014-09-14 09:04:37
【问题描述】:

我正在使用以下代码在 xcode 中创建一个 GIF:

NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil];
NSURL *fileURL = [documentsDirectoryURL URLByAppendingPathComponent:@"animated.gif"];
CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef)fileURL, kUTTypeGIF, imageArray.count, NULL);

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:5];

NSDictionary *frameProperties = [NSDictionary dictionaryWithObject:dict forKey:(NSString *)kCGImagePropertyPNGDictionary];

NSMutableDictionary *dict2 = [NSMutableDictionary dictionaryWithCapacity:5];

const uint8_t colorTable2[9] = {0, 0, 0, 128, 128, 128, 255,255,255};
NSData *colorTableData2 = [NSData dataWithBytes:colorTable2 length:9];

[dict2 setObject:colorTableData2 forKey:(NSString *)kCGImagePropertyGIFImageColorMap];
[dict2 setObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount];
[dict2 setObject:[NSNumber numberWithFloat:0.005] forKey:(NSString *)kCGImagePropertyGIFDelayTime];

NSDictionary *gifProperties = [NSDictionary dictionaryWithObject:dict2 forKey:(NSString *) kCGImagePropertyGIFDictionary];

for (int i = 0; i < imageArray.count; i++) {
    UIImage *image = [UIImage imageWithContentsOfFile:[imageArray objectAtIndex:i]];
    CGImageDestinationAddImage(destination, image.CGImage, (__bridge CFDictionaryRef)frameProperties);
}

CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)gifProperties);
CGImageDestinationFinalize(destination);
CFRelease(destination);

这里的问题是如果 imageArray.count 小于 23,输出的 GIF 将不会包含颜色失真。如果大于 22,颜色失真将以图像块的形式出现,采用背景颜色而不是原始颜色。

我认为我没有设置任何代码来执行图像压缩,所以我不确定为什么会这样。如果有人可以对此问题提供一些见解,那将对我有很大帮助。

编辑: 我保存了每个单独的帧,以确保颜色失真不是由于帧本身造成的。因此我觉得这与 CG 框架有关。

谢谢。

【问题讨论】:

    标签: ios xcode gif


    【解决方案1】:

    尝试使用 ImageIO.framework 框架在 iOS APP 中使用 GIF,我的 GIF 图像也被扭曲了,但后来我使用了这个方法,它们很好

    NSURL *url1 = [[NSBundle mainBundle] URLForResource:@"YourImage" withExtension:@"gif"];
    
    self.imageView.image = [UIImage animatedImageWithAnimatedGIFData:[NSData dataWithContentsOfURL:url1]];
    

    在这之后他们都很好

    【讨论】:

    • 嗨 Geet,我的问题是创建的 GIF 已经失真。直到“UIImage *image = [UIImage imageWithContentsOfFile:[imageArray objectAtIndex:i]];”一切顺利。正如我所看到的,单独保存的图像没有失真。但在“CGImageDestinationFinalize(destination);”之后gif 文件显示不正确的颜色。
    • @Leon 你找到解决方案了吗?
    猜你喜欢
    • 2015-05-28
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    • 2016-02-09
    • 1970-01-01
    • 2012-06-28
    • 2014-02-28
    • 2015-02-27
    相关资源
    最近更新 更多