I am downloading cover images uploaded by App.net users. App.net requires these cover images to be at least 960 pixels wide. I fetch them with a simple AFImageRequestOperation
:
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:URL];
AFImageRequestOperation *imageRequestOperation = [AFImageRequestOperation imageRequestOperationWithRequest:urlRequest success:^(UIImage *image) {
if (completionHandler) {
completionHandler(image); // Load image into UI...
}
}];
[self.fetchQueue addOperation:imageRequestOperation];
This is working, no memory spikes.
I want to cache the authenticated users' images so users don't have to download them each time the app opens. As soon as I archive the downloaded image to disk, I get huge spikes in memory. For example, my cover image is currently 3264 x 2448 pixels. When downloaded on my Mac it comes to around 1,3 MB. However, as soon as I create a NSData object with either UIImagePNGRepresentation(image)
or via TMCache's setObject:forKey:
method, the app's used memory spikes to around 60,0 MB.
For clarity, This is all I'm doing to write the file to disk:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSURL *fileURL = ... // URL of file in "/Application Support"
NSData *imageData = UIImagePNGRepresentation(imageToSave);
[imageData writeToURL:fileURL atomically:YES];
});
Can anyone tell me what is going on? Why is a 1,3 MB being extrapolated into almost sixty times that. How can I avoid this massive and potentially crippling inflation. This is one image, what if the user opens several profiles, each with a cached image?