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

StackOverflow https://stackoverflow.com/questions/23374756

  •  12-07-2023
  •  | 
  •  

문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top