Ios 将UIImage转换为gif时,应用程序崩溃

Ios 将UIImage转换为gif时,应用程序崩溃,ios,gif,Ios,Gif,晚上好,, 我正在尝试用一组UIImage创建gif图像。这是我目前掌握的代码: - (void)stopRecording { NSLog(@"%s", __func__); [_movieWriter finishRecording]; [self stopCamera]; CGFloat second = 5; _gifImages = [NSMutableArray new]; NSString *path = [[NSSearchPathForDirectoriesInDomai

晚上好,, 我正在尝试用一组UIImage创建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");
*/

(lldb)

试试这个,更新你的源代码,然后告诉我们崩溃发生在哪里(设置一个异常断点,这里有很多关于如何做的帖子):

\u gifImages=[NSMutableArray阵列,容量:10];//无论如何,alloc/init或new不是指定的初始值设定项
NSString*path=[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)对象索引:0]stringByAppendingPathComponent:@“movie.mov”];
AVURLAsset*asset1=[[AVURLAsset alloc]initWithURL:[NSURL fileURLWithPath:path]选项:nil];
AVAssetImageGenerator*generate1=[[AVAssetImageGenerator alloc]initWithAsset:asset1];
generate1.appliesPreferredTrackTransform=是;
for(int i=0;i
当有多个图像时,所有图像都有效吗?嗨,托马斯,是的。我检查了一下,alreadyI应该更精确一些。所有
image.CGImage
对象都有效吗?是的。同样,只要我只添加一次,数组中的所有CGImage都可以正常工作。换句话说,如果我做一些类似于(i=3;i<4;i++{…})的事情,效果很好,你能为你的崩溃问题添加完整的回溯吗?
(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
_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]);
}