Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to pre-load 1000's of images using SDWebImage into cache without actually showing them on screen?

Tags:

ios

sdwebimage

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"]];
like image 855
Ethan Allen Avatar asked Apr 29 '14 20:04

Ethan Allen


2 Answers

[[SDWebImagePrefetcher sharedImagePrefetcher] prefetchURLs:<NArray with image URLs>];

This will handle the concurrent download issue for you (maxConcurrentDownloads).

like image 94
CEarwood Avatar answered Sep 20 '22 14:09

CEarwood


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.

like image 41
random Avatar answered Sep 18 '22 14:09

random