Search code examples
swiftuiimageview

Image overflowing bounds with clip to bounds set


I have a UITableViewCell that has an image view within it:

enter image description here

Which has the following bounds for its large image:

enter image description here

And has the following setting:

enter image description here

The code around this is pretty simple, its using the KingFisher library to fetch the image:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let element = comments[indexPath.row]
        ...
        tableView.register(UINib(nibName: "CommentImagePostCell", bundle: nil), forCellReuseIdentifier: "CommentImagePostCell")


        if (element.Image != "" ){
            let cell = tableView.dequeueReusableCell(withIdentifier: "CommentImagePostCell", for: indexPath) as! CommentImagePostCell

            let urlString = API.baseUploadURL + element.Image
            let url = URL(string: urlString)
            cell.imageView!.kf.setImage(with: url)

      ...
     }
     ...
}

However the above code ends up with the image loading in an unclipped manner, seems to complete ignore the bounds? This works else where within my app so I don't think its something wrong with the library and just something I'm missing?

Anyone have any idea why its not clipping?

enter image description here


Solution

  • Common mistake:

    UITableViewCell has its own imageView property and this is what you are currently setting the image to.

    Replace:

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

    With:

    cell.imgView.kf.setImage(with: url)
    

    Rename your UIImageView to something like imgView and update the code, as above, and you should be good.