这是UIImageView
的简单扩展名:
extension UIImageView {
func setImageWithUrl(url: String?) {
setImageWithUrl(url, placeholder: nil)
}
private func setImageWithUrl(url: String?, placeholder: UIImage? = UIImage(), deleteCacheImage: Bool = false) {
image = placeholder ?? image
if let url = url {
let activityIndicatorView = UIActivityIndicatorView(frame: CGRectMake(0, 0, CGRectGetWidth(frame), CGRectGetHeight(frame)))
activityIndicatorView.hidden = false
activityIndicatorView.color = UIColor.curiousBlue()
addSubview(activityIndicatorView)
bringSubviewToFront(activityIndicatorView)
activityIndicatorView.startAnimating()
let options = deleteCacheImage ? SDWebImageOptions.RefreshCached : SDWebImageOptions.LowPriority
sd_setImageWithURL(NSURL(string: url), placeholderImage: placeholder, options: options, completed: { image, error, cache, url in
activityIndicatorView.hidden = true
activityIndicatorView.stopAnimating()
activityIndicatorView.removeFromSuperview()
})
}
}
}
示例image。
但块中的image
为nil
。为什么?它返回一个错误:
下载的图片有0像素
我有SDWebImage
的最新版本,通过cocoapods: 3.7.5 。