title: iOS: frame.size.width/2 doesn't produce a circle on every device
body: <p>I'm aware that the formulae <code>frame.size.width/2</code> should produce a circle border, however in XCode I am currently experiencing some discrepancies.</p>
  
  <p>I have two test devices (iPhone6 and 5th gen iPod touch) I also have the simulator running.  Both my devices display correctly but the simulator draws my circle as a rounded rectangle:</p>
  
  <p><img src="https://i.stack.imgur.com/njNcv.png" alt="enter image description here"></p>
  
  <p>The code I am using to achieve this (although very simple) is:</p>
  
  <pre><code>imgAvatar.layer.masksToBounds = true
  imgAvatar.clipsToBounds = true
  imgAvatar.layer.cornerRadius = imgAvatar.frame.size.width/2
  imgAvatar.layer.borderWidth = 5
  imgAvatar.layer.borderColor = UIColor.whiteColor().CGColor
  </code></pre>
  
  <p>Is there any reason why this is happening? It's driving me insane!</p>
  
  <p><strong>UPDATE</strong> To clear confusion, the <code>UIImageView</code> is declared in my storyboard as <code>190x190</code> it also has a <code>1:1</code> aspect ratio constraint applied to it to ensure it maintains a proportional width and height.</p>
  
  <p><strong>UPDATE 2</strong> To put any suspicions regarding my auto-layout constraints to bed, I have attached the below image which shows the constraints set for <code>imgAvatar</code>. As you can see a the width and height match and the AR is set to ensure it maintains that 1:1 ratio.   I hope that clears up any further doubts</p>
  
  <p><img src="https://i.stack.imgur.com/pOBXQ.png" alt="enter image description here"></p>
  
  <p><strong>ANSWER</strong> Leo pointed out an extremely practical and reusable solution to fix this problem, using Swift extensions one can ensure that a given UIImage is always square, thus always generating a circle, I have posted Leo's solution below:</p>
  
  <pre><code>extension UIImage {
      var circleMask: UIImage? {
          let square = CGSize(width: min(size.width, size.height), height: min(size.width, size.height))
          let imageView = UIImageView(frame: .init(origin: .init(x: 0, y: 0), size: square))
          imageView.contentMode = .scaleAspectFill
          imageView.image = self
          imageView.layer.cornerRadius = square.width/2
          imageView.layer.borderColor = UIColor.white.cgColor
          imageView.layer.borderWidth = 5
          imageView.layer.masksToBounds = true
          UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, false, scale)
          defer { UIGraphicsEndImageContext() }
          guard let context = UIGraphicsGetCurrentContext() else { return nil }
          imageView.layer.render(in: context)
          return UIGraphicsGetImageFromCurrentImageContext()
      }
  }
  
  imgAvatar.image = yourImage.circleMask
  </code></pre>