【问题标题】:Round Image View getting with KingFisher (iOS - Swift)使用 KingFisher 获取圆形图像视图 (iOS - Swift)
【发布时间】:2017-09-20 08:14:41
【问题描述】:

场景:

我有一个@IBOutlet 弱变量 posterImageView: UIImageView!我正在使用 Kingfisher 在我的 tableView 上显示(我必须使用它)。图像来自 API。所以,没关系,图像显示正常,但我想将此图像显示为一个圆圈。我没有这个翠鸟,它工作,使用:

posterImageView.layer.cornedRadius = posterImageView.frame.size.width/2
posterImageView.clipsToBounds = true

但是对于翠鸟,我正在这样做:

let imgStg: String = self.movies[indexPath.row].poster!
let imgURL: URL? = URL(string: imgStg)
let imgSrc = ImageResource(downloadURL: imgURL!, cacheKey: imgStg)

cell.posterImageView.kf.setImage(with: imgSrc)

这个函数内部:

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

所以,当我在 viewDidLoad() 上调用 posterImageView.layer.cornedRadius ... 时,xcode 会说我的 TableViewController 上没有这样的 posterImageView。

如何使用此翠鸟调用自定义图像视图?

【问题讨论】:

    标签: ios swift uiimageview kingfisher


    【解决方案1】:

    我遇到同样的问题,然后我找到了这个解决方案,所以我想分享一下:

    extension UIImageView {
        func download(url: String?, rounded: Bool = true) {
            guard let _url = url else {
                return
            }
            if rounded {
                let processor = ResizingImageProcessor(referenceSize: self.frame.size) |> RoundCornerImageProcessor(cornerRadius: self.frame.size.width / 2)
                self.kf.setImage(with: URL(string: _url), placeholder: nil, options: [.processor(processor)])
            } else {
                self.kf.setImage(with: URL(string: _url))
            }
        }
    }
    

    这里的大秘密是这条指令:

    ResizingImageProcessor(referenceSize: self.frame.size)
    

    【讨论】:

      【解决方案2】:

      如果我理解正确,我认为您应该输入代码以在 cellForRowAtindexPath 方法而不是 viewDidLoad 上获取圆形图像,所以:

        let imgStg: String = self.movies[indexPath.row].poster!
          let imgURL: URL? = URL(string: imgStg)
          let imgSrc = ImageResource(downloadURL: imgURL!, cacheKey: imgStg)
      
          cell.posterImageView.layer.cornedRadius = posterImageView.frame.size.width/2
          cell.posterImageView.clipsToBounds = true
          cell.posterImageView.kf.setImage(with: imgSrc)
      

      编辑:只记得 KingFisher 支持圆形图像:

      let processor = RoundCornerImageProcessor(cornerRadius: 20)
      imageView.kf.setImage(with: url, placeholder: nil, options: [.processor(processor)])
      

      希望这两种方法中的一种对您有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多