SDWebImage渴望加载图片

0xS*_*ina 6 cocoa-touch objective-c ios sdwebimage

我正在努力加载一堆图像:

for (NSDictionary *s in things) {
    [manager downloadWithURL:[NSURL URLWithString:s[photo]]
                     options:0
                    progress:nil
                   completed:nil];
}
Run Code Online (Sandbox Code Playgroud)

它不是下载这些图像.但是,如果我传入一个空的完成块,就像这样:

for (NSDictionary *s in things) {
    [manager downloadWithURL:[NSURL URLWithString:s[photo]]
                     options:0
                    progress:nil
                   completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) { }];
}
Run Code Online (Sandbox Code Playgroud)

然后它工作得很好.我的问题是:为什么?有一个更好的方法吗?传入空座对我来说似乎不对.

Gab*_*lla 19

您使用的API不正确.

要预取图像并将它们存储在缓存中,请使用SDWebImagePrefetcher它.

NSMutableArray * urls = [NSMutableArray arrayWithCapacity:things.count];
for (NSDictionary *s in things) {
    [urls addObject:[NSURL URLWithString:s[photo]]];
}
[[SDWebImagePrefetcher sharedImagePrefetcher] prefetchURLs:urls];
Run Code Online (Sandbox Code Playgroud)

作为旁注,我提交了一个拉取请求 - 刚刚合并 - 以强制执行completedBlock您(错误)使用的API,以便其他程序员不会让您陷入同样的​​错误.

  • 仅供参考,如果您在没有完成块的情况下多次这样做,您可能不想使用`sharedImagePrefetcher`,因为它会在执行新的预取操作之前取消当前正在运行的预取操作.我分配了一个新的,而不是在我的代码中使用共享的. (5认同)