我有一个基于ARC的应用程序,它从Web服务中加载大约2,000个相当大(1-4MB)的Base64编码图像。它将Base64解码的字符串转换为.png
图像文件并将其保存到磁盘。这一切都是在一个循环中完成的,我不应该有任何延续的引用。Objective C UIImagePNGRepresentation内存问题(使用ARC)
我对我的应用程序进行了剖析,发现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。
我试过autoreleasepool,但它没有帮助。出于某种原因,当我尝试使用autoreleasepool进行配置文件时,该应用崩溃了。我会将图片尺寸添加到我的问题中。 – JHollanti 2012-03-20 08:15:49
我将autoreleasepool thingie添加到一堆其他地方,整个事情开始奏效。如果我能够在包含autoreleasepools的时候对应用程序进行配置,那开始的时候会容易得多。 @vikingosegundo曾经有这个我正在面对的分析问题? – JHollanti 2012-03-20 20:26:56
没有。但我经常用lldb调试和分析问题。我切换回gdb。 – vikingosegundo 2012-03-20 20:30:54