在iOS7上创建GIF,在iOS8上打破

时间:2014-09-26 13:23:54

标签: ios ios8 gif

我有一些相当简单的代码,可以从一组UIImages中创建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);

...

执行时没有警告消息也没有错误。 可能导致问题的原因是什么?

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

您需要在添加图片之前设置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);