0

I have a collection view and each it its cell should have their own height based on it the image inside it

here I get data and resize the image based on that

public func configure(with data: Scraper) {
    
    guard let imageURL = URL(string: data.imageURL) else { return }
    
    KingfisherManager.shared.retrieveImage(with: imageURL) { [weak self] result in
        guard let this = self else { return }
        
        if let image = try? result.get().image {
            this.setImageHeight(image: image)
            this.imageView.image = image
        }
    }
}


private func setImageHeight(image: UIImage) {
    let ratio = image.size.height/image.size.width
    imageViewHeight.constant = imageViewWidth * ratio
}

and image constraint


imageViewHeight = NSLayoutConstraint(item: imageView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 0.0, constant: 0)
imageViewHeight.isActive = true

It works well for most of the cell, but some of the cell are not resized until doing more scroll then, they are all fine.

is there any better way to do that?

Thanks

  • Using dynamic height to achieve it. https://stackoverflow.com/questions/44187881/uicollectionview-full-width-cells-allow-autolayout-dynamic-height – Thang Phi Oct 20 '22 at 12:44
  • I use dynamic height, and it works, but not in the very good performance – martin wotterman Oct 20 '22 at 13:00
  • Dynamic height does nothing cause lake performance. Maybe your code somewhere causes that. And it solves this question problem – Thang Phi Oct 20 '22 at 13:31

0 Answers0