title: Masking an image in Swift 2.0
body: <p>I have searched everywhere and found a few examples on how to mask an image, but none of them seem to work for me... I am running on iOS9 and using Swift2.0</p>
  
  <p>Right now this is what I have:</p>
  
  <pre><code>class func maskImage(background: UIImage, withMask mask: UIImage) -&gt; UIImage {
  
      let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedLast.rawValue).rawValue
      let colorSpace = CGColorSpaceCreateDeviceRGB()!
      let context = CGBitmapContextCreate(nil, CGImageGetWidth(mask.CGImage), CGImageGetHeight(mask.CGImage), 8, 0, colorSpace, bitmapInfo)
  
      CGContextDrawImage(context, CGRectMake(0, 0, mask.size.width * mask.scale, mask.size.height * mask.scale), mask.CGImage)
  
      let maskRef: CGImageRef = CGBitmapContextCreateImage(context)!
      let masked: CGImageRef = CGImageCreateWithMask(background.CGImage, maskRef)!
  
      let icon: UIImage = UIImage(CGImage: masked, scale: mask.scale, orientation: mask.imageOrientation)
  
      return icon
  }
  </code></pre>
  
  <p>I am passing two pngs in and getting the error: EXC_BAD_INSTRUCTION on the line where I initialize masked. I don't have enough rep to post the image.</p>
  
  <p>I am probably missing something super simple, but would love your help. Thank you!</p>