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

JHo*_*nti 7 cocoa-touch memory-management objective-c ios automatic-ref-counting

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

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

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

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

我已经尝试过这个没有运气:使用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; 
}
Run Code Online (Sandbox Code Playgroud)

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

Vik*_*ica 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)
    }
}
Run Code Online (Sandbox Code Playgroud)

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