2014-09-13 1 views
2

다음 코드 스 니펫은 UIImage을 사용하여 디스크에 CIImage을 저장합니다.메모리 누수없이 iOS의 파일에 CIImage를 저장할 수 없습니다.

- (void)applicationWillResignActive:(UIApplication *)application 
{ 
    NSString* filename = @"Test.png"; 

    UIImage *image = [UIImage imageNamed:filename]; 

    // make some image processing then store the output 
    CIImage *processedImage = [CIImage imageWithCGImage:image.CGImage]; 

#if 1// save using context 

    CIContext *context = [CIContext contextWithOptions:nil]; 
    CGImageRef cgiimage = [context createCGImage:processedImage fromRect:processedImage.extent]; 
    image = [UIImage imageWithCGImage:cgiimage]; 

    CGImageRelease(cgiimage); 

#else 

    image = [UIImage imageWithCIImage:processedImage]; 

#endif 

    // save the image 

    NSString *filePath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:[@"../Documents/" stringByAppendingString:filename]]; 

    [UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES]; 
} 

그러나, UIImage 거기 CIImage로부터 직접 생성되며 #if 1와 라인 #if 0으로 변경되는 경우가 CGImageRelease

enter image description here

호출하여 해제시에도 CGImageRef 누출 메모리 누수가 없지만 UIImage은 디스크에 저장되지 않습니다.

답변

4

감싸고 오토 릴리즈 풀 내부에 저장하기 :

- (void)applicationWillResignActive:(UIApplication *)application 
{ 
    NSString* filename = @"Test.png"; 

    UIImage *image = [UIImage imageNamed:filename]; 

    // make some image processing then store the output 
    CIImage *processedImage = [CIImage imageWithCGImage:image.CGImage]; 

    @autoreleasepool { 
     CIContext *context = [CIContext contextWithOptions:nil]; 
     CGImageRef cgiimage = [context createCGImage:processedImage fromRect:processedImage.extent]; 
     image = [UIImage imageWithCGImage:cgiimage]; 

     CGImageRelease(cgiimage); 

     // save the image 
     NSURL *documentsDir = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] firstObject]; 
     NSURL *fileURL = [documentsDir URLByAppendingPathComponent:filename]; 

     [UIImagePNGRepresentation(image) writeToURL:fileURL atomically:YES]; 
    } 
} 

은 또한 당신이 아이폰 OS 8 (more info)를 작동하려면 문서 디렉토리를 검색하고 어떻게 업데이트 있습니다.

+0

@autoreleasepool을 사용해도 문제가 해결되지 않습니다. http://stackoverflow.com/questions/32685756/memory-leak-in-cmsamplebuffergetimagebuffer – loretoparisi

관련 문제