Kingfisher and swift 3 - cannot set image with url

隐身守侯 提交于 2019-12-04 17:21:24

Resource is a protocol. URL has been extended to conform to this protocol. So you can do:

let url = URL(string: ...)!
imageView.kf.setImage(with: url)

If you want some control over what Kingfisher uses for the key in its cache, you can use ImageResource:

let identifier = "..."
let url = URL(string: "http://example.com/images/identifier=\(identifier)")!
let resource = ImageResource(downloadURL: url, cacheKey: identifier)

imageView.kf.setImage(with: resource)

I fixed that issue using this:

PhotoHelper.shared.imagePickedBlock = { [weak self] (image, url) in
      self?.imageView.kf.setImage(with: url, placeholder: image, options: nil, progressBlock: nil, completionHandler: { imageResult, error, type, cache in
        self?.imageView.image = image
      })
    }

PhotoHelper is wrapper on native Image Picker:

    func imagePickerController(_ picker: UIImagePickerController,     didFinishPickingMediaWithInfo info: [String : Any]) {
          currentVC.dismiss(animated: true, completion: {
            if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
              let url = info[UIImagePickerControllerReferenceURL] as! URL
              self.imagePickedBlock?(image, url)
             }
          })
      }

For Swift 4.2

import Kingfisher

extension UIImageView {
    func setImage(with urlString: String){
        guard let url = URL.init(string: urlString) else {
            return
        }
        let resource = ImageResource(downloadURL: url, cacheKey: urlString)
        var kf = self.kf
        kf.indicatorType = .activity
        self.kf.setImage(with: resource)
    }
}

How to use

self.imgVw.setImage(with: your image url)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!