Search code examples
iosimagecachingmemorysdwebimage

Memory Management On Large Image Collection Apps


What's the basic idea behind how large apps that contain many images (especially in a table or collection view) manage caching with dequeued cells? No one really answered my previous question about this iOS Memory Warnings and I'm trying to populate a collection view with 100+ images from Parse. After failing to find a good solution with manual code along with down-scaling my images to super low quality, I've tried a number of different libraries, including SDWebImage, LRImageManager, Haneke, and PINRemoteImage. I just want to get a good idea or approach on tackling my app's memory management. How is it that other apps can load hundreds of "fair" quality photos in a dequeue-able view? Is it rocket science?


Solution

  • The answer is a combination of things. Don't load all your images in memory at the same time. Save them as files to your documents or caches directory, and load them for each cell in cellForRowAtIndexPath, and simply discard the in-memory image when the user scrolls it off-screen.

    Also, don't load a large image as a thumbnail. Instead, when you download it, immediately generate a thumbnail version at your target display size and save that (AND the original version, if you need it.) Then you can load and display the thumbnails, with much lower memory impact. Since you're generating the thumbnails for a particular device, you can generate only the thumbnail size you need (retina or non-retina, sized for the screen on the current device. {iPad/4" phone, 5" phone, iPhone 6, 6+, etc})