Search code examples
iosfilememorymemory-leaksnsfilemanager

Loading large images from disk in ios app


Currently I am downloading images of size 8 mb and storing them in the document path inside the app. During testing process, I noticed that the memory use increases a lot when fetching the images from the document and show them in the collection view which is affecting the app performance and causing memory errors. Memory errors appear when I fetch the images more then a time. The search and fetch are done using the NSFileManager. Any suggestion that help me solving this issue?


Solution

  • Fundamentally, you're seeing high memory use because your app is designed to use a lot of memory. Loading a bunch of large images at the same time is going to be memory intensive.

    The first thing to try: Don't use the full 8MB images in the collection view. When you download the images, create smaller scaled versions. Use the scaled images in the collection view. Make the size of the scaled image match the image size used in the collection view. (There are many, many code examples for image scaling, try Google and you'll find them). Keep the full size images in files so you can load them when necessary, but use the smaller thumbnails whenever possible.