我使用 SDWebImage与 progress .
我在 `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/