圆形图像查看与KingFisher(iOS - Swift)

时间:2017-04-23 18:05:55

标签: ios swift uiimageview kingfisher

情景:

我有一个@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。

如何通过此翠鸟电话自定义图像视图?

2 个答案:

答案 0 :(得分:9)

如果我理解正确,我认为您应该输入代码以在 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)])

希望这两种方式中的一种适合你。

答案 1 :(得分:0)

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

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)