Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 847
U23r Avatar asked Nov 30 '22 09:11

U23r


1 Answers

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.

like image 160
JP Aquino Avatar answered Dec 15 '22 17:12

JP Aquino