晚上好, 我正在尝试用一组UIImages创建一个gif图像。这是我到目前为止的代码:
- (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");
*/
}
每次运行应用程序时,它都会在CGImageDestinationFinalize(目标)(EXEC_BAD_ACCESS)上崩溃。 当我在数组中添加多个UIImage时,它只会崩溃。只有一个UIImage转换为gif工作正常。
真的很感激任何帮助......
回溯:
(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)
答案 0 :(得分:0)
试试这个,更新您的源代码,然后告诉我们崩溃发生的位置(设置一个异常断点,这里有很多关于如何做的帖子):
_gifImages = [NSMutableArray arrayWithCapacity:10]; // whatever, alloc/init or new is not a designated initializer
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;
for (int i = 0; i < second; i++) {
CMTime time = CMTimeMake(i, second);
NSError *err = NULL; // moved into the loop
CGImageRef oneRef = [generate1 copyCGImageAtTime:time actualTime:NULL error:&err];
if(oneRef) { // test on the returned object, not the error
NSLog(@"Will create image...");
UIImage *image = [[UIImage alloc] initWithCGImage:oneRef];
assert(image); // if no image returned, then you have a problem
CFRelease(oneRef); // got to free it, its core foundation
NSLog(@"did create image. Will write image...");
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil); // you should use the nil parameters to get success of failure indication
NSLog(@"...did write image");
[_gifImages addObject:image];
}
else NSLog(@"Error: %@", [err localizedDescription]);
}