ios - 每个图像调用 4 次 SDWebImage Complete 闭包

标签 ios swift sdwebimage

我使用 SDWebImageprogress .

我在 `init()`` 构造函数中使用了以下代码:

if let url: NSURL = NSURL(string: previewCard.getImageUrls().getWithInt(0) as! String) {
  self.imageView?.setImageWithURL(url, placeholderImage: MGImage.imageWithColor(UIColor.clearColor()), options: SDWebImageOptions.RefreshCached, completed: { (image:UIImage!, error:NSError!, type:SDImageCacheType, loadUrl:NSURL!) -> Void in

    println("-------------- done")
    }, usingActivityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)

}

控制台输出为:

 println("-------------- done")
 println("-------------- done")
 println("-------------- done")
 println("-------------- done")

为什么完整的闭包调用了四次?如何防止它被调用四次?

编辑:我验证了 init() 方法只被调用了一次。无论我在哪里调用 setImageWithUrl 方法,完整的闭包至少被调用两次。

最佳答案

这是因为您指定了“RefreshCached”。查看文档 here .特别是 SDWebImageRefreshCached 下的这一行:

the completion block is called once with the cached image and again with the final image

所以你应该总是至少得到两次回调。因为你得到四个回调而不是两个,我猜你要么得到错误(所以检查“错误”参数是否告诉你任何有用的东西),或者你正在刷新大图像并在此过程中获得一些进度更新.无论如何,最后一个回调将是包含您想要的刷新图像的回调。

关于ios - 每个图像调用 4 次 SDWebImage Complete 闭包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31292184/

相关文章:

ios - 如何在swift3中datePicker索引 Realm 并绘制图表

objective-c - SDWebImage 下载图像并存储到缓存中以获取 key

ios - 如何改善图像加载时间?

swift - SceneKit 如何在不同高度的地形上移动角色

ios - 将 SDWebImage 用于带有可选图像 URL 的 UITableViewCell

ios - UITableView 行数像 iOS 邮件应用

ios - 自适应阈值 OpenCV IOS

ios - 如何删除所有 UserDefaults 数据? - swift

iphone - 获取联系人图片 ios

ios - 添加标题 View 后,滚动到 UITableView 的底部导致 NSException