[英]SDWebImage eager load images
我正在努力加载一堆图像:
for (NSDictionary *s in things) {
[manager downloadWithURL:[NSURL URLWithString:s[photo]]
options:0
progress:nil
completed:nil];
}
它不是下载这些图像。 但是,如果我传入一个空的完成块,就像这样:
for (NSDictionary *s in things) {
[manager downloadWithURL:[NSURL URLWithString:s[photo]]
options:0
progress:nil
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) { }];
}
然后它工作得很好。 我的问题是:为什么? 有一个更好的方法吗? 传入空座对我来说似乎不对。
您使用的API不正确。
要预取图像并将其存储在缓存中,请使用SDWebImagePrefetcher
。
NSMutableArray * urls = [NSMutableArray arrayWithCapacity:things.count];
for (NSDictionary *s in things) {
[urls addObject:[NSURL URLWithString:s[photo]]];
}
[[SDWebImagePrefetcher sharedImagePrefetcher] prefetchURLs:urls];
作为旁注,我提交了一个拉取请求 - 刚刚合并 - 以强制您使用的API中存在已completedBlock
的块,以便其他程序员不会让您陷入同样的错误。
如果仔细查看-[SDWebImageManager downloadWithURL:options:progress:completed:]
实现 ,您将找到以下行:
if (!url || !completedBlock || (!(options & SDWebImageRetryFailed) && isFailedUrl))
{
if (completedBlock)
{
// Complain about invalid URL, completely irrelevant to us at this point.
...
}
return operation;
}
所以是的,如果completionBlock
nil
,它什么都不做。 为什么? 可能是SDWebImage
开发人员认为该方法在没有传递参数的情况下无用。 你最好创建一个GitHub问题来问他们。
SDWebImage修复了完成块问题,现在只需一行Swift即可:
SDWebImagePrefetcher.shared().prefetchURLs(urlArray)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.