I have an ARC-based application that downloads about 2,000 fairly large (1-4 MB) Base64 encoded images from a web service. It converts Base64 decoded strings to .png
image files and saves them to disk. All this is done in a loop where I should not have any lingering links.
I profiled my application and found out that the UIImagePNGR view clogs about 50% of the available memory.
As I see it, UIImagePNGR represents the caching of the images they create. One way to fix this is to clear this cache. Any ideas how to do this?
Another solution would be to use something other than a UIImagePNGRview?
I already tried this with no luck: The memory issue when using UIImagePNGR is . Not to mention the fact that I really can’t use the provided solution, because it will make my application too slow.
This is the method that I call from my loop. UIImage is an image converted from 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; }
EDIT: Image sizes range from approximately 1000 * 800 to 3000 * 2000.
source share