2013-11-21 31 views
0

晚上好, 我試圖用一組UIImages創建一個gif圖像。這是我的代碼至今:將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"); 
*/ 

}

每次我運行它崩潰的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

當有多個圖像,都是th e圖像有效嗎? – ThomasW

+0

嗨托馬斯,是的,他們是。我檢查了已經 – Pascal

+0

我應該更精確。所有的'image.CGImage'對象都是有效的嗎? – ThomasW

回答

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]); 
} 
相關問題