0

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

Keith
  • 1,969
  • 4
  • 17
  • 27
  • Write your constraint adding code in main queue. – Sagar Chauhan May 04 '18 at 05:39
  • Thanks. I just tried that with DispatchQueue.main.async {self.contentView.addConstraint(constraint)} The images are sized correctly but there is a really big gap in-between them now. – Keith May 05 '18 at 08:07
  • Try by writing full code of `if image != nil { //Full code }` in main queue. Because you have tried to take `UIImage`'s size. – Sagar Chauhan May 05 '18 at 08:11
  • I just tried that, the same issue. I put a height on the image view of 300, it fixes the gap but does make a space at the bottom of the page. I guess that's better than in the middle. Not ideal, but a hack until I can figure it out. – Keith May 05 '18 at 08:29

0 Answers0