![](/img/trans.png)
[英]How should I pre-load all images and cache them using AFNetworking?
[英]Is there a way to pre-load 1000's of images using SDWebImage into cache without actually showing them on screen?
我正在使用SDWebImage下载和缓存图像。 我想将许多图像预先加载到缓存中。
是否有一种简单的方法可以在不必向用户显示图像的情况下执行此操作? 我目前正在使用此代码进行显示:
[anImageView setImageWithURL:[NSURL URLWithString:@"http://www.sameple.com/myimage.jpg"]
placeholderImage:[UIImage imageNamed:@"loadingicon.png"]];
[[SDWebImagePrefetcher sharedImagePrefetcher] prefetchURLs:<NArray with image URLs>];
这将为您处理并发下载问题( maxConcurrentDownloads
)。
SDWebImageManager是UIImageView + WebCache类别背后的类。 它将异步下载程序与映像缓存存储区联系起来。 您可以直接使用此类从Web图像下载中获益,而不是使用UIView(即:使用Cocoa)在其他上下文中进行缓存。
这是一个如何使用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
}
}];
您可以为每个图像运行并执行此操作...我不确定1000个图像的性能如何,您需要确保并警告您的用户您将要执行的操作。
另一种方法与坚持SDWebImage
将管理自己的NSOperationQueue
的SDWebImageDownloaderOperation
并以此从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;
这样可以让您更好地控制已经执行的并发下载操作的数量以及更好的状态保存控制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.