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 i开发者_如何学GomageNamed:@"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.
You can't. imageWithContentsOfFile:
will always load the image from file (though lazily). What you can do is create an in memory cache of you own with NSArrays or NSDictionaries, depending on how you'll want to do the lookup.
精彩评论