【问题标题】:Create GIF works on iOS7, breaks on iOS8创建 GIF 在 iOS 7 上工作,在 iOS 8 上中断
【发布时间】:2014-09-26 13:23:54
【问题描述】:

我有一些相当简单的代码,可以从一组 UIImage 中创建一个 GIF 图像。 它在 iOS7 上运行良好。在 iOS8 上创建了一个文件,但它以某种方式损坏(在设备上或下载到桌面时不起作用)。下面是示例代码:

...

float frameTime = (1/(fps * speed));

NSString *fileOutput = [NSString stringWithFormat:@"%@/%ld", NSTemporaryDirectory(), time(NULL)];

CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef)[NSURL fileURLWithPath:fileOutput],kUTTypeGIF,[images count],NULL);

NSDictionary *frameProperties = [NSDictionary dictionaryWithObjectsAndKeys:[NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:frameTime] forKey:(NSString *)kCGImagePropertyGIFDelayTime],(NSString *)kCGImagePropertyGIFDictionary,kCFBooleanFalse,kCGImagePropertyHasAlpha,[NSNumber numberWithFloat:1.0],kCGImageDestinationLossyCompressionQuality, nil];

NSMutableDictionary *gifProps = [NSMutableDictionary dictionary];
[gifProps setObject:[NSNumber numberWithBool:NO] forKey:(NSString *)kCGImagePropertyGIFHasGlobalColorMap];
[gifProps setObject:[NSNumber numberWithInt:0] forKey:(NSString*)kCGImagePropertyGIFLoopCount];
[gifProps setObject:[NSNumber numberWithBool:NO] forKey:(NSString *)kCGImagePropertyHasAlpha];
[gifProps setObject:[NSNumber numberWithFloat:1.0] forKey:(NSString*)kCGImageDestinationLossyCompressionQuality];

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

for (int i = 0; i<[images count]; i++)
{
    UIImage *im = [UIImage imageNamed:[images objectAtIndex:i]];

    @autoreleasepool
    {
        CGImageDestinationAddImage(destination, im.CGImage, (__bridge CFDictionaryRef)frameProperties);
        im = nil;
    }
}

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

...

没有警告信息,执行时也没有错误。 什么可能导致问题?

【问题讨论】:

  • 我在 iOS 8.1.3 上看到了同样的情况,解决方法是将 kCGImagePropertyGIFHasGlobalColorMap 设置为 true。但是,内存要大得多,而且我在应用程序扩展中的用例使得在没有受到监视的情况下无法使用。如果您有任何更新,请告诉我们。
  • 由于颜色映射实际上没有正确设置,内存使用量可能会大得多。一条红鲱鱼。看我的回答。

标签: ios ios8 gif


【解决方案1】:

Apple 确认这是一个 iOS8 错误,已提交 (18512475)。

【讨论】:

  • 来自苹果的任何更新,无论此错误是否会被修复或是否有解决方法。
  • 我遇到了同样的问题。奇怪的是,我在 iPhone 上没有问题(测试 5、5C、6、6 Plus),但 iPad(Air,mini 2)无法创建具有相同设置的 GIF。我的解决方法是将 kCGImagePropertyGIFHasGlobalColorMap 设置为 YES。然而,这使用了更多的堆内存(峰值内存为 115 MB,而 20 帧的峰值内存为 5 MB,每个帧的大小为 640x480)。我也很快尝试使用 ANGif 库,但它需要很长时间并使用大约相同的内存。公平地说,我还没有尝试过优化它。
  • 苹果有什么更新吗? 18512475 是什么?我用上面的号码搜索过。但我找不到任何东西..
【解决方案2】:

您需要在添加图像之前设置 GIF 属性。有一种颜色映射算法,它期望能够查看图像以正确创建颜色映射。

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

CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)gifProperties);


for (int i = 0; i<[images count]; i++)
{
    UIImage *im = [UIImage imageNamed:[images objectAtIndex:i]];

    @autoreleasepool
    {
        CGImageDestinationAddImage(destination, im.CGImage, (__bridge CFDictionaryRef)frameProperties);
        im = nil;
    }
}

CGImageDestinationFinalize(destination);
CFRelease(destination);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-28
    • 1970-01-01
    • 2014-11-04
    • 2015-04-22
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    相关资源
    最近更新 更多