How to fix UIImageView Aspect Fit after download

229 Views Asked by At

I'm setting the image view to aspect-fit and resizing it after the image is downloaded, to remove the extra padding on top and bottom. What I have works fine unless the first image doesn't fill the whole screen, it will crop the second image incorrectly. If I back out of the screen and go back in it's fixed. Any idea how to fix this? I've added an estimated row height as well.

Inside my cell:

let mainImageUrl = URL(string: noteImage!.imageUrl)
        self.noteImageView.kf.indicatorType = .activity
        self.noteImageView.kf.setImage(with: mainImageUrl, completionHandler: {
            (image, error, cacheType, imageUrl) in
            if image != nil {
                let constraint = NSLayoutConstraint(
                    item: self.noteImageView, attribute: NSLayoutAttribute.height,
                    relatedBy: NSLayoutRelation.equal,
                    toItem: self.contentView, attribute: NSLayoutAttribute.width,
                    multiplier: (image!.size.height - 30) / (image!.size.width), constant: 0.0)
                self.contentView.addConstraint(constraint)
            } else {
                print("error downloading image")
            }
        })

Here is what it looks like, scenario 1 with a portrait tall image works fine. Scenario 2 with a short image crops it weird.

aspect fit crop problem

0

There are 0 best solutions below