ios - SDWebImage 急切加载图像

标签 ios objective-c cocoa-touch sdwebimage

我正在尝试加载一堆图像:

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

它不是在下载这些图像。但是,如果我传入一个空的完成 block ,如下所示:

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

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

最佳答案

您使用的 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,这样其他程序员就不会让你犯同样的错误。

关于ios - SDWebImage 急切加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18509624/

相关文章:

ios - 带有 AccessoryView 的单个 UITableViewCell

ios - 为什么我的应用程序因卖家和艺术家名称不匹配而被 App Store 拒绝?

ios - 部分图片在 iOS 中可点击

ios - 社交共享图像的图像质量

iphone - Xcode 从一个 View 移动到另一个 View

iphone - 从 UIViewController A 到 B 并获得反馈

ios - Xamarin中,如何处理WKWebView ShouldStartLoad事件

ios - 向 UITextView 添加手势会取消默认行为

ios - 有没有像CSS text-transform :uppercase for iOS UILabel这样的东西

ios - 如何在没有导航 Controller 的情况下导航到另一个 Controller ?