【发布时间】: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。但是,内存要大得多,而且我在应用程序扩展中的用例使得在没有受到监视的情况下无法使用。如果您有任何更新,请告诉我们。
-
由于颜色映射实际上没有正确设置,内存使用量可能会大得多。一条红鲱鱼。看我的回答。