6

This is my simple extension for 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()
        })
    }
}
}

Example image.

but image in block is nil. Why? It returns an error:

Downloaded image has 0 pixels

I have the latest version of SDWebImage, through cocoapods: 3.7.5.

Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358

0 Answers0