title: Edit image into circle shape and insert into another image in Swift
body: <p>I have <code>image1</code>, I would like to make that image circle shape and then insert it into <code>image2</code>. How to do it using swift? I can't do this with photoshop or other image editing tool, because my <code>image1</code> will may be different every time.</p>
  
  <p><strong>EDIT:</strong> This what I am trying to achieve:
  <a href="https://i.stack.imgur.com/pnsXZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pnsXZ.png" alt="enter image description here"></a></p>
  
  <p>Flower is <code>image1</code>.</p>
  
  <p><strong>EDIT 2:</strong> What I have tried and kind of works:</p>
  
  <pre><code>let image2 = UIImage(named: "RedPin")
  let newSize = CGSizeMake(image2!.size.width, image2!.size.height)
  UIGraphicsBeginImageContext(newSize)
  
  image2!.drawInRect(CGRectMake(0,0,newSize.width,newSize.height))
  
  let imageView: UIImageView = UIImageView(image: image1)
  var layer: CALayer = CALayer()
  layer = imageView.layer
  
  layer.masksToBounds = true
  layer.cornerRadius = CGFloat(65)
  
  UIGraphicsBeginImageContext(imageView.bounds.size)
  layer.renderInContext(UIGraphicsGetCurrentContext()!)
  let roundedImage = UIGraphicsGetImageFromCurrentImageContext()
  UIGraphicsEndImageContext()
  
  // decrease top image size
  roundedImage.drawInRect(CGRectMake(10,10,45,45), blendMode: .Normal, alpha:1.0)
  
  let createdNewImage = UIGraphicsGetImageFromCurrentImageContext()
  </code></pre>
  
  <p>Yet <code>image1</code> is more a square with rounded edges and in general image seems to be low quality. Here is what that function above does:</p>
  
  <p><a href="https://i.stack.imgur.com/4Rc6z.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4Rc6z.png" alt="enter image description here"></a></p>
  
  <p><strong>EDIT 3:</strong> <a href="https://github.com/gavinbunney/Toucan" rel="nofollow noreferrer">Toucan</a> library is great at creating rounded images (<code>let roundedImage = Toucan(image: newImage).maskWithEllipse().image</code>), however final image version still looks blurry. Why's that?</p>