2013-11-21 4 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 이미지가 유효합니까? – 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]); 
} 
관련 문제