![](/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.