Caching images -- NSURLRequest and cachePolicy Vs Custom code
I have several UITableViews that asynchronously load the images, and text from the internet. I want to cache images to speed up performance.
What would be best:
* Using NSURLRequest and开发者_高级运维 cachePolicy?
* Saving the image to file system?
For NSURLRequest and cachePolicy case, how would I clean the images if I do not want to use them.
NSURLRequest should work fine with the default cache policy, as far as only loading the image once per run (i.e. it's cached in memory). The iPhone does not cache data to disk, though.
The docs are a bit conflicting on this point. For example, the docs on NSURLCache initWithMemoryCapacity:diskCapacity:diskPath say that diskPath on iOS should be the name of a subdirectory in which to store the on-disk cache.
But the documentation on NSURLCacheStoragePolicy says:
Important: iOS ignores this cache policy, and instead treats it as NSURLCacheStorageAllowedInMemoryOnly.
Based on actual testing, the latter appears to be the case. So no matter what options you set, you won't get caching to disk on iOS (currently). So if you want your image to stick around between runs, you'll need to save it to the file system.
Some have recommended https://github.com/rs/SDURLCache for this purpose, though I haven't yet tried it myself.
精彩评论