Objective C UIImagePNGRepresentation内存问题(使用ARC)

时间:2012-03-19 22:04:22

标签: objective-c ios cocoa-touch memory-management automatic-ref-counting

我有一个基于ARC的应用程序,可以从Web服务加载大约2,000个相当大(1-4MB)的Base64编码图像。它将Base64解码后的字符串转换为.png图像文件并将其保存到磁盘。这都是在循环中完成的,我不应该有任何遗留的引用。

我描述了我的应用程序并发现UIImagePNGRepresentation占用了大约50%的可用内存。

我看到它的方式,UIImagePNGRepresentation正在缓存它创建的图像。解决此问题的一种方法是刷新缓存。任何想法如何做到这一点?

另一种解决方案是使用UIImagePNGRepresentation以外的其他东西吗?

我已经尝试过这个没有运气了:Memory issue in using UIImagePNGRepresentation。更不用说我不能真正使用提供的解决方案,因为它会使我的应用程序太慢。

这是我从循环调用的方法。 UIImage是从Base64转换的图像:

+ (void)saveImage:(UIImage*)image:(NSString*)imageName:(NSString*)directory {
  NSData *imageData = UIImagePNGRepresentation(image); //convert image into .png format.
  NSFileManager *fileManager = [NSFileManager defaultManager];
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it
  NSString *documentsDirectory = [paths objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory
  NSString *pathToFolder = [documentsDirectory stringByAppendingPathComponent:directory];

  if (![fileManager fileExistsAtPath:pathToFolder]) {
    if(![fileManager createDirectoryAtPath:pathToFolder withIntermediateDirectories:YES attributes:nil error:NULL]) {
       // Error handling removed for brevity
    }
  }

  NSString *fullPath = [pathToFolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]]; //add our image to the path
  [fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; //finally save the path (image)

  // clear memory (this did nothing to improve memory management)
  imageData = nil;
  fileManager = nil; 
}

编辑: 图像尺寸大致在1000 * 800到3000 * 2000之间。

7 个答案:

答案 0 :(得分:4)

您可以通过自动释放池

包装方法体
+ (void)saveImage:(UIImage*)image:(NSString*)imageName:(NSString*)directory {

    @autoreleasepool
    {
        NSData *imageData = UIImagePNGRepresentation(image); //convert image into .png format.
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it
        NSString *documentsDirectory = [paths objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory
        NSString *pathToFolder = [documentsDirectory stringByAppendingPathComponent:directory];

        if (![fileManager fileExistsAtPath:pathToFolder]) {
          if(![fileManager createDirectoryAtPath:pathToFolder withIntermediateDirectories:YES attributes:nil error:NULL]) {
             // Error handling removed for brevity
          }
        }

        NSString *fullPath = [pathToFolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]]; //add our image to the path
        [fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; //finally save the path (image)
    }
}

但实际上,如果你向我们提供更多数字,它可能会有所帮助:
图像有多大的尺寸。这很重要,因为图像数据以原始像素存储在存储器中。我是一张图片2000px width * 2000px height * 4 Bytes (RGBA) ~ 15MB。现在想象一下,转换算法必须存储每个像素或至少某个区域的信息。预计会有大量的数字。

答案 1 :(得分:1)

我对UIImagePNGRepresentation和ARC也有同样的问题。 我的项目正在生成磁贴,即使UIImagePNGRepresentation调用是@autoreleasepool的一部分,UIImagePNGRepresentation也没有删除分配的内存。

我没有幸运的是,通过添加更多@ autoreleasepool,就像JHollanti那样,问题就消失了。

我的解决方案基于EricS的想法,使用ImageIO Framework保存png文件:

-(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);
}

}

最重要的是之后发布图像:CGImageRelease(image);

答案 2 :(得分:0)

是否有必要在此转换数据,可能是从磁盘加载转换它?

NSData对象可能使它成为NSMutableData,因为它的内存被分配一次并根据需要增长。

答案 3 :(得分:0)

The ImageIO Framework(PDF)可能会更好运。它比UIKit对内存和缓存有更多的控制。

答案 4 :(得分:0)

试试这个:

+ (void)saveImage:(UIImage*)image:(NSString*)imageName:(NSString*)directory {
  NSData *imageData = UIImagePNGRepresentation(image); //convert image into .png format.
  CFDataRef imageDataRef = (__bridge_retained CFDataRef) imageData; // ARC fix
  NSFileManager *fileManager = [NSFileManager defaultManager];
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it
  NSString *documentsDirectory = [paths objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory
  NSString *pathToFolder = [documentsDirectory stringByAppendingPathComponent:directory];

  if (![fileManager fileExistsAtPath:pathToFolder]) {
    if(![fileManager createDirectoryAtPath:pathToFolder withIntermediateDirectories:YES attributes:nil error:NULL]) {
       // Error handling removed for brevity
    }
  }

  NSString *fullPath = [pathToFolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]]; //add our image to the path
  [fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; //finally save the path (image)

  // clear memory (this did nothing to improve memory management)
  imageData = nil;
  CFRelease(imageDataRef); // ARC fix
  fileManager = nil;
}

答案 5 :(得分:0)

其中一种可能的方法是使用github的libs从Internet下载和缓存UIImage / NSData。它可以通过SDWebImage(https://github.com/rs/SDWebImage)或APSmartStorage(https://github.com/Alterplay/APSmartStorage)。最新从互联网获取图像并将其巧妙地存储在磁盘和内存中。

答案 6 :(得分:0)

我通过发送4通道图像(RGBA或RGBX)而不是3通道图像(RGB)来解决此问题。 您可以检查是否有机会更改图像的参数。

将Base64转换为UIImage时,请尝试使用kCGImageAlphaNoneSkipLast代替kCGImageAlphaNone