title: merge two different images swift
body: <p>I'm facing the following problem : I have to merge two images A and B to create a new image C as a result of the merging.
  The first image (A) is a .png and its a picture frame for the second one (B) which I took. How can I merge this two images in a new one (C) which have the same size of the first one (A)?</p>
  
  <p>I've try this code: </p>
  
  <pre><code>var bottomImage: UIImage = polaroidFoto.image!
  var image: UIImage = immaginePropria.image!
  var newSize: CGSize = CGSizeMake(300, 300)
  UIGraphicsBeginImageContext(newSize)
  bottomImage.drawInRect(CGRectMake(0, 0, newSize.width, newSize.height))
  image.drawInRect(CGRectMake(0, 0, newSize.width, newSize.height), blendMode: kCGBlendModeNormal, alpha: 0.8) //0.8 default
  var newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
  UIGraphicsEndImageContext()
  </code></pre>
  
  <p>But this one return to me a new image (C) but with alpha effect on it, i want the image (A) stay over the image (B) and merge this two into a new one (C)! Thank you!</p>
  
  <p><strong>UPDATE</strong>
  Can someone try to correct this code:</p>
  
  <pre><code>func maskImage(image: UIImage, withMask maskImage: UIImage) -&gt; UIImage {
  var colorSpace: CGColorSpaceRef = CGColorSpaceCreateDeviceRGB()
  var maskImageRef: CGImageRef = maskImage.CGImage()
  var mainViewContentContext: CGContextRef = CGBitmapContextCreate(Nil, maskImage.size.width, maskImage.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedFirst)
  CGColorSpaceRelease(colorSpace)
  if mainViewContentContext == nil {
      return nil
  }
  var ratio: CGFloat = 0
  ratio = maskImage.size.width / image.size.width
  if ratio * image.size.height &lt; maskImage.size.height {
      ratio = maskImage.size.height / image.size.height
  }
  var rect1: CGRect = 00maskImage.size.widthmaskImage.size.height
  var rect2: CGRect = -((image.size.width * ratio) - maskImage.size.width) / 2-((image.size.height * ratio) - maskImage.size.height) / 2image.size.width * ratioimage.size.height * ratio
  CGContextClipToMask(mainViewContentContext, rect1, maskImageRef)
  CGContextDrawImage(mainViewContentContext, rect2, image.CGImage)
  var newImage: CGImageRef = CGBitmapContextCreateImage(mainViewContentContext)
  CGContextRelease(mainViewContentContext)
  var theImage: UIImage = UIImage.imageWithCGImage(newImage)
  CGImageRelease(newImage)
  return theImage }
  </code></pre>
  
  <p>This one may be useful but the syntax is full of errors! Thank you!</p>