Search code examples
ioscollectionviewnscache

NSCache with hundreds of images


I got a collectionView which displays 9 images (200x300px) at a time. The total number can go up to hundreds, though.

Currently I check in cellForItem if an Image is in the cache. If not, I'll load it from the file system and put it in the cache.

The problem: the first time I scroll through the collection it will drop frames because it is still loading the images from the file system. After one initial scroll-through it's butter smooth. (That's on a 5s)

I was wondering if it would be good practice to preload all (or the first x) the images in to the cache?

I don't see a big problem with the 5s but I fear it will crush older hardware.

Any thoughts?


Solution

  • Try to perform your disk loading mechanism in the background. You could set up a queue with NSOperationQueue filled with loading operations.

    Using NSCache with preloading mechanisms does not make that much sense, since even if you preload all images, the system can and will evict the cache if it sees fit. In that case, the work you did with preloading was to no avail.