Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using system cache of UIImage objects with images loaded from disk cache (NSCachesDirectory)?

I'm trying to improve scrolling performance on a UITableView that uses cells with images fetched from the web, but stored in the NSCachesDirectory. The cells have a custom content view to draw the contents (an image).

When I use a placeholder image from the app bundle, using [UIImage imageNamed:@"Placeholder.png"], scrolling performance is super fast.

When I load an image from the disk cache (NSCachesDirectory) using [UIImage imageWithContentsOfFile:cachePath], scrolling performance gets worse.

According to the documentation, imageNamed: caches the image and imageWithContentsOfFile: does not.

How to use UIImage's system cache when using imageWithContentsOfFile: ?

Thanks a bunch!

like image 781
Martijn Thé Avatar asked Feb 21 '10 16:02

Martijn Thé


2 Answers

It seems to be possible to use the path to an image in the NSCachesDirectory as argument for the [UIImage imageNamed:] method. The method accepts relative paths (relative to the app bundle), e.g.: @"../Library/Caches/SomeCachedImage.png" works.

UIImage automatically caches the image in memory if it is used multiple times, which improves the performance when an image is used multiple times in a table view.

like image 191
Martijn Thé Avatar answered Nov 14 '22 04:11

Martijn Thé


The problem is most likely that you are loading and decompressing the image in the main run loop. This will block the user interface for a short time. You get much better performance if you do the loading and decompression in a seperate thread and only set the image in the main loop. (Which is also required for user interface changes, which setting an image on a UIImageView is)

This will require some more infrastructure. Like for example a notification scheme or key value observing.

like image 21
Stefan Arentz Avatar answered Nov 14 '22 05:11

Stefan Arentz