2

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

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

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

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

我不知道这个资源应该如何工作,因为我在文档中找不到任何东西。

4

3 回答 3

6

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)
于 2016-11-24T20:01:45.227 回答
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)
             }
          })
      }
于 2018-10-21T07:38:23.863 回答
0

对于斯威夫特 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)
于 2019-02-04T09:01:27.007 回答