Good evening, I'm trying to create a gif images with a set of UIImages. This is the code i have so far:
- (void)stopRecording
{
NSLog(@"%s", __func__);
[_movieWriter finishRecording];
[self stopCamera];
CGFloat second = 5;
_gifImages = [NSMutableArray new];
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"movie.mov"];
AVURLAsset *asset1 = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:path] options:nil];
AVAssetImageGenerator *generate1 = [[AVAssetImageGenerator alloc] initWithAsset:asset1];
generate1.appliesPreferredTrackTransform = YES;
NSError *err = NULL;
for (int i = 0; i < second; i++) {
CMTime time = CMTimeMake(i, second);
CGImageRef oneRef = [generate1 copyCGImageAtTime:time actualTime:NULL error:&err];
if (!err) {
UIImage *image = [[UIImage alloc] initWithCGImage:oneRef];
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
[_gifImages addObject:image];
}
else NSLog(@"Error: %@", [err localizedDescription]);
}
NSString *gifPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"test.gif"];
_gifURL = [[NSURL alloc] initFileURLWithPath:gifPath];
NSDictionary *fileProperties = @{(__bridge id)kCGImagePropertyGIFDictionary:
@{(__bridge id)kCGImagePropertyGIFLoopCount:
@(0), // 0 means loop forever
}};
NSDictionary *frameProperties = @{
(__bridge id)kCGImagePropertyGIFDictionary: @{
(__bridge id)kCGImagePropertyGIFDelayTime: @(0.2f), // a float (not double!) in seconds, rounded to centiseconds in the GIF data
}
};
NSMutableData *data = [NSMutableData new];
// CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef)_gifURL, kUTTypeGIF, 2, NULL);
CGImageDestinationRef destination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)(data), kUTTypeGIF, 2, NULL);
for (NSUInteger i = 1; i < 3; i++) {
UIImage *image = _gifImages[i];
NSLog(@"ImageSize: %@", NSStringFromCGSize(image.size));
CGImageDestinationAddImage(destination, image.CGImage, (__bridge CFDictionaryRef)frameProperties);
}
CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)fileProperties);
CGImageDestinationFinalize(destination);
CFRelease(destination);
/*
bool success = [SSCameraHelper createGifImageFromImages:_cgImages toFilePath:fileURL];
if (success) NSLog(@"Images converted to gif");
else NSLog(@"There was an error converting the images");
*/
}
Every time i run the application it crashes on CGImageDestinationFinalize(destination) (EXEC_BAD_ACCESS). It only crashes when i add more than one UIImage in the array. Having only one UIImage converted to gif works just fine.
Any help is truly appreciated...
Backtrace:
(lldb) bt
* thread #1: tid = 0x1332, 0x300948f4 ImageIO`_cg_EGifPutLine + 76, queue = 'com.apple.main-thread, stop reason = EXC_BAD_ACCESS (code=1, address=0x8437000)
frame #0: 0x300948f4 ImageIO`_cg_EGifPutLine + 76
frame #1: 0x30084db8 ImageIO`_CGImagePluginWriteGIF + 5304
frame #2: 0x30059476 ImageIO`CGImageDestinationFinalize + 66
frame #3: 0x000f5318 Snapshot3`-[SSCameraViewController stopRecording](self=0x14563da0, _cmd=0x320c72b5) + 2296 at SSCameraViewController.m:375
frame #4: 0x2fcd9cdc Foundation`__NSFireTimer + 64
frame #5: 0x2f2bfe7e CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 14
frame #6: 0x2f2bfa9a CoreFoundation`__CFRunLoopDoTimer + 794
frame #7: 0x2f2bde22 CoreFoundation`__CFRunLoopRun + 1218
frame #8: 0x2f228470 CoreFoundation`CFRunLoopRunSpecific + 524
frame #9: 0x2f228252 CoreFoundation`CFRunLoopRunInMode + 106
frame #10: 0x33f3c2ea GraphicsServices`GSEventRunModal + 138
frame #11: 0x31add844 UIKit`UIApplicationMain + 1136
frame #12: 0x000e3e84 Snapshot3`main(argc=1, argv=0x27d72c58) + 116 at main.m:16
(lldb)