2014-07-14 2 views
2

를 사용하는 동안의 malloc을 방지하는 방법 :다음 스택의 질문을 사용하여 프로그래밍 방식 아이폰 OS에서 GIF를 만들기 위해 노력하고 CGImageDestinationFinalize

Create and and export an animated gif via iOS?

내 코드는 다음과 같습니다

// File Parameters 
const void *keys[] = { kCGImagePropertyGIFLoopCount }; 
const void *values[] = { (CFNumberRef) 0 }; 

CFDictionaryRef params   = CFDictionaryCreate(NULL, keys, values, 1, NULL, NULL); 
const void *keys2[] = { kCGImagePropertyGIFDictionary }; 
const void *values2[] = { (CFDictionaryRef) params }; 

CFDictionaryRef fileProperties = CFDictionaryCreate(NULL, keys2 , values2, 1, NULL, NULL); 

// URL to the documents directory 
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil]; 
NSURL *fileURL    = [documentsDirectoryURL URLByAppendingPathComponent:fileName]; 

// Object that writes GIF to the specified URL 
CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef)fileURL, kUTTypeGIF, [arrayOfAllFrames count], NULL); 
CGImageDestinationSetProperties(destination, fileProperties); 

for (NSUInteger i = 0; i < [arrayOfAllFrames count]; i++) { 
    @autoreleasepool { 
     float delayTime = [[gifFramesDuration objectAtIndex:i] floatValue]; 
     NSDictionary *frameProperties = @{ 
              (__bridge id)kCGImagePropertyGIFDictionary: @{ 
                (__bridge id)kCGImagePropertyGIFDelayTime: [NSNumber numberWithFloat:delayTime] // a float (not double!) in seconds, rounded to centiseconds in the GIF data 
                } 
              }; 

     UIImage *myImage = [arrayOfAllFrames objectAtIndex:i]; 
     CGImageDestinationAddImage(destination, myImage.CGImage, (__bridge CFDictionaryRef)frameProperties); 
    } 
} 
if (!CGImageDestinationFinalize(destination)) { 
    NSLog(@"failed to finalize image destination"); 
} 
CFRelease(destination); 
CFRelease(fileProperties); 
CFRelease(params); 

그러나 한 번 GIF 파일에 약 240 프레임을 추가하려고하면 CGImageDestinationFinalize가 호출되면 디버거에서 다음 오류가 발생합니다.

(923,0xb0115000) malloc: *** error for object 0xd1e7204: incorrect checksum for freed object - object was probably modified after being freed. 

일부 해결 방법을 제공하거나 malloc을 피하는 방법에 대한 제안을 제공해 주시겠습니까?

답변

0

우선, Instruments를 사용하여 앱을 디버깅 해보십시오. 아마도이 문제가이 메서드로 인해 발생한다는 것을 알 수 있습니다. Generatefromrgbimagewu

내 스레드 구현에 원인이 있는지 궁금해했는데, 그런 오류가 발생하면 이미지 크기 조정.

이미지의 크기를 조정하면 위의 코드에서 자신의 GIF가 생성됩니다.

+0

어떻게 이미지의 크기를 조정할 수 있습니까? –