iosswiftuiimageviewkingfisher

Round Image View getting with KingFisher (iOS - Swift)


Scenario:

I have a @IBOutlet weak var posterImageView: UIImageView! that I'm showing on my tableView using Kingfisher (I have to use it). The image comes from an API. So, it's all right, the image is showing ok, but I want to show this image as a circle. I did without this kingfisher and it worked, using:

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

But with kingfisher I'm doing this:

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)

Inside this function:

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

So, when I call posterImageView.layer.cornedRadius ... on my viewDidLoad(), xcode say that there is no such posterImageView on my TableViewController.

How can I custom my image view with this kingfisher call?


Solution

  • If I am understanding correctly I think you are supposed to enter the code to get the rounded image on your cellForRowAtindexPath method not viewDidLoad, so:

      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)
    

    Edit: Just remembered that KingFisher has support for rounded images:

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

    Hope one of the two ways works for you.