我正在尝试以编程方式在iOS中创建GIF,使用以下堆栈的问题:
Create and and export an animated gif via iOS?
我的代码如下所示:
// File Parameters
const void *keys[] = { kCGImagePropertyGIFLoopCount };
const void *values[] = { (CFNumberRef) 0 };
CFDictionaryRef params = CFDictionaryCreate(NULL, keys, values, 1, NULL, NULL);
const void *keys2[] = { kCGImagePropertyGIFDictionary };
const void *values2[] = { (CFDictionaryRef) params };
CFDictionaryRef fileProperties = CFDictionaryCreate(NULL, keys2 , values2, 1, NULL, NULL);
// URL to the documents directory
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil];
NSURL *fileURL = [documentsDirectoryURL URLByAppendingPathComponent:fileName];
// Object that writes GIF to the specified URL
CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef)fileURL, kUTTypeGIF, [arrayOfAllFrames count], NULL);
CGImageDestinationSetProperties(destination, fileProperties);
for (NSUInteger i = 0; i < [arrayOfAllFrames count]; i++) {
@autoreleasepool {
float delayTime = [[gifFramesDuration objectAtIndex:i] floatValue];
NSDictionary *frameProperties = @{
(__bridge id)kCGImagePropertyGIFDictionary: @{
(__bridge id)kCGImagePropertyGIFDelayTime: [NSNumber numberWithFloat:delayTime] // a float (not double!) in seconds, rounded to centiseconds in the GIF data
}
};
UIImage *myImage = [arrayOfAllFrames objectAtIndex:i];
CGImageDestinationAddImage(destination, myImage.CGImage, (__bridge CFDictionaryRef)frameProperties);
}
}
if (!CGImageDestinationFinalize(destination)) {
NSLog(@"failed to finalize image destination");
}
CFRelease(destination);
CFRelease(fileProperties);
CFRelease(params);
但是,一旦我尝试向GIF文件添加大约240帧,调试器会在调用CGImageDestinationFinalize后抛出以下错误:
(923,0xb0115000) malloc: *** error for object 0xd1e7204: incorrect checksum for freed object - object was probably modified after being freed.
您能否请我提供一些解决方法,或者建议如何避免使用malloc?
答案 0 :(得分:0)
首先,尝试使用Instruments调试您的应用。您可能会注意到该问题是由该方法引起的: Generatefromrgbimagewu
我一直想知道原因是否在我的线程实现中,但事实证明,一旦出现这种错误,你应该专注于调整Image的大小。
图片调整后,上面发布的代码会生成您自己的GIF。