UIImage保存到PNG导致内存泄漏

时间:2016-06-17 01:32:57

标签: ios objective-c memory-leaks uiimage cgimageref

我想将NSArray中的UIImage保存到本地作为PNG。但是,当我在for循环中使用UIImagePNGRepresentation时,即使已经存在@autoreleasepool,内存也会大大增长。

for (int i = 0; i < array.count; i++) {

    @autoreleasepool {
        NSDictionary *src = array[i];

        NSString *localPath = [SPLDPath stringByAppendingPathComponent:@"realImg"];
        NSFileManager *file = [NSFileManager defaultManager];
        if (![file fileExistsAtPath:localPath]) {
            [file createDirectoryAtPath:localPath withIntermediateDirectories:NO attributes:nil error:nil];
        }

        NSString *screenShotImg = [localPath stringByAppendingPathComponent:[NSString stringWithFormat:@"ScreenShot_%d.png", i]];
        NSData *PNGData = UIImagePNGRepresentation(src[@"image"]);
        [PNGData writeToFile:screenShotImg atomically:YES];
    }
}

所以我尝试将UIImage转换为CGImageRef并使用ImageIO框架来保存图像。然后使用CGImageRelease()在每个循环中释放内存。

-(void)saveImage:(CGImageRef)image directory:(NSString*)directory filename:(NSString*)filename  {
@autoreleasepool {
    CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", directory, filename]];
    CGImageDestinationRef destination = CGImageDestinationCreateWithURL(url, kUTTypePNG, 1, NULL);
    CGImageDestinationAddImage(destination, image, nil);

    if (!CGImageDestinationFinalize(destination))
        NSLog(@"ERROR saving: %@", url);

    CFRelease(destination);
    CGImageRelease(image);
}

}

for (int i = 0; i < array.count; i++) {

   NSDictionary *src = array[i];

   NSString *localPath = [SPLDPath stringByAppendingPathComponent:@"realImg"];
        NSFileManager *file = [NSFileManager defaultManager];
   if (![file fileExistsAtPath:localPath]) {
       [file createDirectoryAtPath:localPath withIntermediateDirectories:NO attributes:nil error:nil];
   }
   NSString *fileName = [NSString stringWithFormat: @"ScreenShot_%d.png", i];

   CGImageRef cgRef=[src[@"image"] CGImage];
   [self saveImage:(cgRef) directory:localPath filename:fileName];

} enter image description here 记忆力下降,然而,一个新问题发生了。由于内存过度释放,我的应用程序崩溃了。因为UIImage是由CGImageRelease()发布的,但是ARC也试图在应用程序结束之前向僵尸对象发送delloc消息。 如何释放CGImageRef但不与ARC发生碰撞?

1 个答案:

答案 0 :(得分:0)

不要在CGImage属性上调用CGImageRelease(),因为您的代码不包含该对象的incr引用。如果您的代码在某处明确地增加了ref,那么您只会在这种情况下释放。