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!
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With