简体   繁体   English

有没有办法使用SDWebImage将1000个图像预先加载到缓存中而不在屏幕上实际显示它们?

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

I am using SDWebImage to download and cache images. 我正在使用SDWebImage下载和缓存图像。 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>];

这将为您处理并发下载问题( maxConcurrentDownloads )。

The SDWebImageManager is the class behind the UIImageView+WebCache category. SDWebImageManager是UIImageView + WebCache类别背后的类。 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). 您可以直接使用此类从Web图像下载中获益,而不是使用UIView(即:使用Cocoa)在其他上下文中进行缓存。

Here is a simple example of how to use SDWebImageManager: 这是一个如何使用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. 您可以为每个图像运行并执行此操作...我不确定1000个图像的性能如何,您需要确保并警告您的用户您将要执行的操作。

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. 另一种方法与坚持SDWebImage将管理自己的NSOperationQueueSDWebImageDownloaderOperation并以此从SDImageCache来存储他们,因为他们完成。

/**
 * 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. 取自GitHub页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM