The code uses a NSMutableDictionary *thumbnailCache
to cache UIImage instances. The code assumes that in the app bundle, there's a directory thumbnails
with scaled down versions of their images.
The method now first looks in the thumbnailCache
dictionary whether the thumbnail for the given image (which is only a filename without full path, e. g. myimage.png
). If the dictionary did not contain an image already, the image is loaded from the thumbnails
directory (using imageWithContentsOfFile:
instead of imageNamed:
, since the authors claim the later causes trouble). The loaded image is then stored in the dictionary so the next time the app asks for the thumbnail, it can use the already loaded instance.
For this code to work correctly in your app, you need to add a thumbnails
folder to your project. When you add it to your project, be sure to select "Create folder references for any added folders" instead of the default "Create groups for any added folders". Only then you will get a subdirectory in your app's main bundle, otherwise all files are put into the same top-level folder.
But the whole point is that the author claims:
- Avoid
[UIImage imageNamed:]
.
- Instead, have a
NSMutableDictionary
.
- Look up images in the dictionary.
- If found, use that.
- If not found, load image using
[UIImage imageWithContentsOfFile:]
to manually load the image and store it in the dictionary.