I am using SDWebImage to download and cache images. I'd like to pre-load many of the images into cache.
Is there an easy way to do this without having to actually display the image to the user? I am currently using this code for displaying:
[anImageView setImageWithURL:[NSURL URLWithString:@"http://www.sameple.com/myimage.jpg"]
placeholderImage:[UIImage imageNamed:@"loadingicon.png"]];
[[SDWebImagePrefetcher sharedImagePrefetcher] prefetchURLs:<NArray with image URLs>];
This will handle the concurrent download issue for you (maxConcurrentDownloads
).
The SDWebImageManager is the class behind the UIImageView+WebCache category. It ties the asynchronous downloader with the image cache store. You can use this class directly to benefit from web image downloading with caching in another context than a UIView (ie: with Cocoa).
Here is a simple example of how to use SDWebImageManager:
SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadWithURL:imageURL
options:0
progress:^(NSInteger receivedSize, NSInteger expectedSize)
{
// progression tracking code
}
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished)
{
if (image)
{
// do something with image
}
}];
You could run through and do this for every image...i'm not sure how the performance would be for 1000's of images and you will want to make sure and warn your user what your about to do.
Another approach sticking with SDWebImage
would be to manage your own NSOperationQueue
of SDWebImageDownloaderOperation
and use this from SDImageCache
to store them as they finish.
/**
* Store an image into memory and optionally disk cache at the given key.
*
* @param image The image to store
* @param key The unique image cache key, usually it's image absolute URL
* @param toDisk Store the image to disk cache if YES
*/
- (void)storeImage:(UIImage *)image forKey:(NSString *)key toDisk:(BOOL)toDisk;
This would give you a little more control of how many concurrent download operations you had going as well as better state preservation control.
Taken from GitHub page.
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