pgaspar
pgaspar

Reputation: 143

UIImageView+AFNetworking on device memory problems for large images (leak?)

I've been having issues loading this 2.8MB image with AFNetworking's UIImageView category.

When I run the app on my iPad mini it crashes before it's able to display the image. I've created a sample app that does only that (load and display the image) in order to pinpoint the problem. You can download it here.

Problematic Image:

Here are my results on Instruments:

Image: http://www.nasa.gov/images/content/712130main_8246931247_e60f3c09fb_o.jpg (2.8MB)

Instruments window for the app with the problematic image

Using the Activity Monitor instrument I got this (seemingly absurd) memory result: 187MB real mem / 535 virtual mem.

Activity Monitor instrument for the problematic image

Working example:

Below are the results for another (bigger) image from the same site.

Image: http://www.nasa.gov/sites/default/files/2013-3051.jpg (5MB)

Instruments window for the app with the ok image

And with Activity Monitor:

Activity Monitor instrument for the ok image

Using the Simulator:

On the simulator, the first image does not crash the app, but it still has a weird pattern when compared to the working image:

Problematic Image:

Instruments for simulator with problematic image

Working Image:

Instruments for simulator with ok image

Setup details:

I can't figure out what's wrong with the first image and why it's blowing up memory like that. I did notice it has a lot of pixels (12150×6075), although I don't know if that's relevant.

Upvotes: 1

Views: 839

Answers (1)

Rob
Rob

Reputation: 438092

While I think the UIImageView category of AFNetworking is pretty weak, I think some (if not most) of the blame here rests with these huge images. Just because the JPEG files are reasonably sized, it doesn't mean that the resulting bitmaps (and thus, presumably, the resulting UIImage objects) are also reasonably sized.

That first image enjoys a lot of compression, and the resulting bitmap is at least an order of magnitude larger than the original JPEG. The second JPEG enjoys decent compression, too, but not nearly as dramatic as the first JPEG, and therefore the second JPEG's bitmap isn't as big as the first one's.

When dealing with images of this size, you'd either have the server resize them for screen resolutions, or employ some tiling solution. Devices of limited memory cannot be expected to handle images of these sizes gracefully.

Upvotes: 1

Related Questions