title: How to resize UIImageView after loading image from URL w/ Kingfisher
body: <p>I would like to resize any downloaded images so they maintain their aspect ratio, however are all as wide as the <code>UITableViewCell</code> they are rendered in.</p>
  
  <p>My UIImageView is configured with the <code>contentMode</code> as <code>AspectFit</code> and I have the following anchors on my cell:</p>
  
  <p><a href="https://i.stack.imgur.com/Zh07Y.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Zh07Y.png" alt="enter image description here"></a></p>
  
  <p>I am almost there with the current attempt:</p>
  
  <pre><code>class AnimatedImageTableViewCell: UITableViewCell {
      @IBOutlet weak var gifView: AnimatedImageView!
      @IBOutlet weak var imageHeightAnchor: NSLayoutConstraint!
  
      var refreshCell: (() -&gt; Void)?
  
      func render(imageUrl: String) {
          guard let url = URL(string: imageUrl) else { return }
  
          gifView.kf.setImage(with: url) { result in
              switch result {
              case .success(let value):
                  let ratio = value.image.size.width / value.image.size.height
                  let newHeight = self.gifView.frame.width / ratio
                  self.imageHeightAnchor.constant = newHeight
                  self.refreshCell?()
              case .failure(let error):
                  print(error) // The error happens
              }
          }
      }
  }
  </code></pre>
  
  <p>Also:</p>
  
  <pre><code>class HomeViewController: UITableViewController {
  
      let images = [
          "https://cdn-images-1.medium.com/max/1600/1*OJxJTJLSyqJ0nMeuswuCSQ.gif",
          "https://static1.squarespace.com/static/552a5cc4e4b059a56a050501/565f6b57e4b0d9b44ab87107/566024f5e4b0354e5b79dd24/1449141991793/NYCGifathon12.gif",
          "https://media2.giphy.com/avatars/100soft/WahNEDdlGjRZ.gif"
      ]
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          tableView.tableFooterView = UIView()
          tableView.estimatedRowHeight = 200
          tableView.rowHeight = UITableView.automaticDimension
          tableView.allowsSelection = false
  
          let nib = UINib(nibName: "AnimatedImageTableViewCell", bundle: nil)
          tableView.register(nib, forCellReuseIdentifier: "cellId")
  
      }
  
      override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
          return images.count
      }
  
  
      override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
          let imagePath = images[indexPath.item]
  
          let cell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath) as! AnimatedImageTableViewCell
  
          cell.refreshCell = {
              tableView.reloadRows(at: [indexPath], with: .automatic)
          }
  
          cell.render(imageUrl: imagePath)
  
          return cell
      }
  }
  </code></pre>
  
  <p>However in some case the cell is too tall. The images renders full width, with the correct aspect ratio, there is just way too much space above and below the image.</p>
  
  <p>I also get errors in the console as follows:</p>
  
  <pre><code>(
      "&lt;NSLayoutConstraint:0x600000329770 Kingfisher.AnimatedImageView:0x7fd339e19330.height == 268.5   (active)&gt;",
      "&lt;NSLayoutConstraint:0x60000032a8f0 Kingfisher.AnimatedImageView:0x7fd339e19330.top == UITableViewCellContentView:0x7fd339e1a280.topMargin + 8   (active)&gt;",
      "&lt;NSLayoutConstraint:0x60000032a850 UITableViewCellContentView:0x7fd339e1a280.bottomMargin == Kingfisher.AnimatedImageView:0x7fd339e19330.bottom + 8   (active)&gt;",
      "&lt;NSLayoutConstraint:0x60000032a7b0 'UIView-bottomMargin-guide-constraint' V:[UILayoutGuide:0x600001908ee0'UIViewLayoutMarginsGuide']-(8)-|   (active, names: '|':UITableViewCellContentView:0x7fd339e1a280 )&gt;",
      "&lt;NSLayoutConstraint:0x600000323980 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fd339e1a280.height == 450.5   (active)&gt;",
      "&lt;NSLayoutConstraint:0x60000032a6c0 'UIView-topMargin-guide-constraint' V:|-(8)-[UILayoutGuide:0x600001908ee0'UIViewLayoutMarginsGuide']   (active, names: '|':UITableViewCellContentView:0x7fd339e1a280 )&gt;"
  )
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/UL1fo.png" rel="noreferrer"><img src="https://i.stack.imgur.com/UL1fo.png" alt="enter image description here"></a></p>