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

我有一个基于ARC的应用程序,从Web服务加载大约2000个相当大(1-4MB)的Base64编码图像。 它将Base64解码的string转换为.png图像文件并将其保存到磁盘。 这一切都在一个循环,我不应该有任何挥之不去的引用。

我分析了我的应用程序,发现UIImagePNGRepresentation占用了大约50%的可用内存。

我看到它的方式,UIImagePNGRepresentationcaching它创build的图像。 解决这个问题的一个方法是刷新caching。 任何想法如何可以做到这一点?

另一种解决scheme是使用UIImagePNGRepresentation以外的东西?

我已经试过这个没有运气: 使用UIImagePNGRepresentation内存问题 。 更何况,我不能真正使用那里提供的解决scheme,因为它会使我的应用程序太慢。

这是我从我的循环调用的方法。 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不等。

你可以通过autorelease池来包装方法体

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

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

我与UIImagePNGRepresentation和ARC有同样的问题。 即使UIImagePNGRepresentation调用是@autoreleasepool的一部分,我的项目正在生成瓷砖,UIImagePNGRepresentation所分配的内存也不会被删除。

我没有幸运的问题是通过添加一些@ autoreleasepool像JHollanti一样消失了。

我的解决scheme基于EricS的想法,使用ImageIO框架来保存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);

是否有必要在这里转换数据,也许转换从磁盘加载?

NSData对象可能会这样NSMutableData这样的内存分配一次,并根据需要增长。

ImageIO框架 (PDF)可能会带来更好的运气。 它比UIKit有更多的内存和caching控制。

尝试这个:

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

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

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

当您将Base64转换为UIImage时,请尝试使用kCGImageAlphaNoneSkipLast而不是kCGImageAlphaNone