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

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

我有一个基于 ARC 的应用程序,它从网络服务加载大约 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 不等。

最佳答案

你可以用自动释放池包装方法体

+ (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 宽度 * 2000px 高度 * 4 字节 (RGBA) ~ 15MB。现在想象一下,转换算法必须为每个像素或至少某些区域存储信息。预计会有巨大的数字。

关于objective-c - Objective C UIImagePNGRepresentation 内存问题(使用 ARC),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9778646/

相关文章:

ios - 在第一次运行时加载不同的 View

ios - 如何在 iOS 中设置 x、y、高度、宽度和旋转的动画?

ios - 设备锁定时 UILocalNotification 不触发

iphone - 查找谁拥有对象的保留计数

ios - 当我使用委托(delegate)将值传递给另一个 ViewController 时,我发现委托(delegate)什么都不做

objective-c - 在 Mac 应用程序中移动 shell 脚本创建的文件

objective-c - 我动态填充 NSTableView 的方式感觉不太对劲

iphone - 在 Objective-C 中处理数组的更好方法?

ios - Xcode在实现一个类后不断重建

ios - 转向横向Xcode时如何更改文本字段的大小