Swift – Dynamic UIImageView Size Within UITableView

I’m looking for your help on this topic as I’ve been struggling quite a while.
I want to implement a TableView with a Custom TableViewCell showing an image.

To make this simple, I simply put a UIImageView inside a tableviewcell using autolayout (illustrated below).
enter image description here

  • Efficiently copying Swift Array to memory buffer for iOS Metal
  • Use iPhone as a game controller in a tvOS app?
  • Cordova 6.3.1 on iOS 10 and Xcode 8
  • Why does the status bar appear when popoverPresentationController is called in Swift?
  • How to select lines of text in a PDF and then highlight them? (iOS)
  • How to use image as button in ionic
  • What I want is to display the image inside a UIImageView however those images dimensions can be anything and are inconsistent (portrait, landscape, square) …

    Therefore, I’m looking to display an image with a fixed width (the width of the device) and a dynamic height that respect the ratio of the images. I want something like this :
    enter image description here

    I unfortunately didn’t manage to reach that result.

    Here’s how I implemented it – (I’m using Haneke to load the image from an URL – image stored in Amazon S3) :

    class TestCellVC: UITableViewCell {
    
    
    @IBOutlet weak var selfieImageView: UIImageView!
    @IBOutlet weak var heightConstraint: NSLayoutConstraint!
    
    func loadItem(#selfie: Selfie) {        
        let selfieImageURL:NSURL = NSURL(string: selfie.show_selfie_pic())!
    
        self.selfieImageView.hnk_setImageFromURL(selfieImageURL, placeholder: nil, format: HNKFormat<UIImage>(name: "original"), failure: {error -> Void in println(error)
            }, success: { (image) -> Void in
                // Screen Width
                var screen_width = UIScreen.mainScreen().bounds.width
    
                // Ratio Width / Height
                var ratio =  image.size.height / image.size.width
    
                // Calculated Height for the picture
                let newHeight = screen_width * ratio
    
                // METHOD 1
                self.heightConstraint.constant = newHeight
    
                // METHOD 2
                //self.selfieImageView.bounds = CGRectMake(0,0,screen_width,newHeight)
    
                self.selfieImageView.image = image
            }
        )
    
    }
    

    }

    override func viewDidLoad() {
        super.viewDidLoad()
    
        // Register the xib for the Custom TableViewCell
        var nib = UINib(nibName: "TestCell", bundle: nil)
        self.tableView.registerNib(nib, forCellReuseIdentifier: "TestCell")
    
        // Set the height of a cell dynamically
        self.tableView.rowHeight = UITableViewAutomaticDimension
        self.tableView.estimatedRowHeight = 500.0
    
        // Remove separator line from UITableView
        self.tableView.separatorStyle = UITableViewCellSeparatorStyle.None
    
        loadData()
    
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCellWithIdentifier("TestCell") as TestCellVC
    
    
        cell.loadItem(selfie: self.selfies_array[indexPath.row])
        // Remove the inset for cell separator
        cell.layoutMargins = UIEdgeInsetsZero
        cell.separatorInset = UIEdgeInsetsZero
    
        // Update Cell Constraints
        cell.setNeedsUpdateConstraints()
        cell.updateConstraintsIfNeeded()
        cell.sizeToFit()
    
        return cell
    }
    

    My calculation of the dynamic Height is correct (I’ve printed it). I’ve tried both method (describe in the code) but none of them worked :

    1. Set the Height Autolayout constraint of the UIImageView
    2. Modify the frame of the UIImageView

    See Results of Method 1 and 2 here :
    image
    Image 2

    Please, Help me !! Thanks in advance.

    3 Solutions Collect From Internet About “Swift – Dynamic UIImageView Size Within UITableView”

    In the storyboard add trailing, leading, bottom and top constraints to your UIImageView. After you load your image all you need to do is add an aspect constraint to your UIImageView. Your images cell would look something like this:

    class ImageTableViewCell: UITableViewCell {
    
        @IBOutlet weak var customImageView: UIImageView!
    
        internal var aspectConstraint : NSLayoutConstraint? {
            didSet {
                if oldValue != nil {
                    customImageView.removeConstraint(oldValue!)
                }
                if aspectConstraint != nil {
                    customImageView.addConstraint(aspectConstraint!)
                }
            }
        }
    
        override func prepareForReuse() {
            super.prepareForReuse()
            aspectConstraint = nil
        }
    
        func setCustomImage(image : UIImage) {
    
            let aspect = image.size.width / image.size.height
    
            let constraint = NSLayoutConstraint(item: customImageView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: customImageView, attribute: NSLayoutAttribute.height, multiplier: aspect, constant: 0.0)
            constraint.priority = 999
    
            aspectConstraint = constraint
    
            customImageView.image = image
        }
    }
    

    You can check working example here DynamicTableWithImages

    If you want the UIImageView to tell the UITableView how tall it needs to be, then you need to configure the table view to enable automatic row calculation. You do this by setting estimatedRowHeight to a fixed positive value and setting rowHeight to UIViewAutomaticDimension. That’s part one.

    Now you need to configure the UIImageView to request a height based on the width which the table view requires and on the aspect ratio of the image. This will be part two.

    First, set the contentMode to .AspectFit. This will cause the view to resize the appearance of the image based on whatever dimensions it takes. However, this still doesn’t cause it to request the needed height with auto layout. Instead, it will request the height of the intrinsicContentSize, which may be quite different from the resized image. So, second, add a constraint to the UIImageView that is of the form width = height * multiplier, where the multiplier is based on the aspect ratio of the image itself.

    Now the table view will require the correct width, the contentMode mechanism will ensure the image is resized correctly without distortion, and the aspect ration constraint will ensure that the image view requires the correct height via auto layout.

    This works. The downside of it is that you will need to update the aspect ratio constraint every time you assign a new image to the image view, which could be quite often if the image view is in a cell that’s getting re-used.

    An alternative approach is to add only a height constraint and update it in the layoutSubviews of a view containing the image view. This has better performance but worse code modularity.

    Remove the height constraint from your imageView. Choose the contentMode of image view that starts with “aspect” eg: aspect fill or aspect fit (as it fits according to your requirements).

    NOTE: You also need to set the height of the row so that the imageView fits in it. Use

    heightForRowAtIndexPath
    

    to set custom row height.