翠鸟和迅捷3 - 无法用网址设置图像

时间:2016-11-24 19:49:07

标签: swift3 kingfisher

检查文档和迁移指南,我应该能够使用以下代码设置新图像:

imageView.kf.setImage(with:url ...) 

但实际上我在库中找不到这个方法,我只看到:

imageView.kf.setImage(with:Resource... )

我不确切知道这个资源是如何工作的,因为我在文档中找不到任何内容。

3 个答案:

答案 0 :(得分:3)

Resource是一种协议。 URL已扩展为符合此协议。所以你可以这样做:

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

如果您想要控制Kingfisher用于缓存中密钥的内容,可以使用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)

答案 1 :(得分:0)

我使用以下方法解决了该问题:

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是本机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)
             }
          })
      }

答案 2 :(得分:0)

对于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)
    }
}

使用方法

self.imgVw.setImage(with: your image url)