Q:

How to merge two UIImages? 

I am trying to merge two different images and create a new one.  This is the way I would like to do:
I have this image (A): 
It's a PNG image and I would like to merge this one with another image (B) which I took from the phone to create something like this:

I need a function who merge A with B creating C. The size must remain from the A image and the image B should auto adapt the size to fit into the polaroid (A).  Is it possible to do that?  Thank for your help! 
UPDATE
Just one thing, the image (A) is a square and the image i took is a 16:9, how can i fix that??  If i use your function the image (B) that i took become stretched! 

A:

Hope this may help you,
var bottomImage = UIImage(named: "bottom.png")
var topImage = UIImage(named: "top.png")

var size = CGSize(width: 300, height: 300)
UIGraphicsBeginImageContext(size)

let areaSize = CGRect(x: 0, y: 0, width: size.width, height: size.height)
bottomImage!.draw(in: areaSize)

topImage!.draw(in: areaSize, blendMode: .normal, alpha: 0.8)

var newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()! 
UIGraphicsEndImageContext()

All the Best :)

A:

This way the overlay picture will be much cleaner
class func mergeImages(imageView: UIImageView) -> UIImage {
 UIGraphicsBeginImageContextWithOptions(imageView.frame.size, false, 0.0) 
 imageView.superview!.layer.renderInContext(UIGraphicsGetCurrentContext()!) 
 let image = UIGraphicsGetImageFromCurrentImageContext()
 UIGraphicsEndImageContext()

 return image 
}

A:

Swift 4 UIImage extension that enables easy image merging / overlaying. 
extension UIImage { 

  func overlayWith(image: UIImage, posX: CGFloat, posY: CGFloat) -> UIImage {
    let newWidth = size.width < posX + image.size.width ?  posX + image.size.width : size.width
    let newHeight = size.height < posY + image.size.height ?  posY + image.size.height : size.height
    let newSize = CGSize(width: newWidth, height: newHeight)

    UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
    draw(in: CGRect(origin: CGPoint.zero, size: size))
    image.draw(in: CGRect(origin: CGPoint(x: posX, y: posY), size: image.size))
    let newImage = UIGraphicsGetImageFromCurrentImageContext()! 
    UIGraphicsEndImageContext()

    return newImage
  }

}

