title: Rounded corners on custom UITableViewCell image
body: <p>I'm trying to apply a radius to the corner of the images showed in a table to make them look like a circle. The problem is that the images are located via autolayout, so the final size is not calculated till the view has layout all its subviews, and I need to code the circle mask after that.</p>
  
  <p>The solution that came to me was to write the code inside the method tableView(... willDisplayCell ...), or inside the methods didMoveToSuperview() and layoutSubviews() on the custom UITableViewCell subclass. I have seen this solutions on some questions of this forum, but no one of them are working.</p>
  
  <p>Here is the code with one of those methods commented:</p>
  
  <pre><code>import UIKit
  
  struct Contact {
      var name: String
      var image: UIImage
  }
  
  class ActiveChatsController: UITableViewController {
  
      let contacts = [
          Contact(name: "Alex", image: UIImage(named: "Alex")!),
          Contact(name: "Puto Albert", image: UIImage(named: "Albert")!),
          Contact(name: "Golfo-man", image: UIImage(named: "Pablo")!)
      ]
  
      override func viewDidLoad() {
          self.navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: AppDesign.color(withIntensity: 6)]
      }
  
      override func numberOfSections(in tableView: UITableView) -&gt; Int {
          return 1
      }
  
      override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
          return contacts.count
      }
  
      override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
          let cell = Bundle.main.loadNibNamed("OpenedChatCell", owner: self, options: nil)?.first as! OpenedChatCell
          cell.contactImage.image = contacts[indexPath.row].image
          cell.contactName.text = contacts[indexPath.row].name
  
          return cell
      }
  
  }
  </code></pre>
  
  <p>And the custom cell subclass is got from a XIB file:</p>
  
  <pre><code>import UIKit
  
  class OpenedChatCell: UITableViewCell {
  
      @IBOutlet weak var contactImage: UIImageView!
      @IBOutlet weak var contactName: UILabel!
  
      /*
      override func didMoveToSuperview() {
          self.contactImage.layer.cornerRadius = self.contactImage.frame.width / 2
          self.contactImage.clipsToBounds = true
      }
       */
  
  }
  </code></pre>
  
  <p>If I run these codes on the simulator, I get this:</p>
  
  <p><a href="https://i.stack.imgur.com/oW3wp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oW3wp.png" alt="enter image description here"></a></p>
  
  <p>But if I delete the comments on the didMoveToSuperview() method and let it change the radius I get this:</p>
  
  <p><a href="https://i.stack.imgur.com/Lw3fN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Lw3fN.png" alt="enter image description here"></a></p>
  
  <p>After this, I wrote inside didMoveToSuperview():</p>
  
  <pre><code>print(self.contactImage.frame.height)
  </code></pre>
  
  <p>And it shows a height of 177.0, so I don't know where the error may be.</p>