我将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
方法,都会至少调用两次完整的闭包。
答案 0 :(得分:1)
It's because you're specifying "RefreshCached". Check out the documentation here. Specifically this line under SDWebImageRefreshCached:
the completion block is called once with the cached image and again with the final image
So you should always get at least two callbacks. Since you're getting four callbacks rather than two, I would guess you're either getting errors (so check if the "error" param tells you anything useful), or you're refreshing large images and getting some progress updates along the way. Regardless, the last callback will be the one that contains the refreshed image you want.