title: Image masking fails on iOS10 beta3
body: <p>For some time now we use the following code to mask a grayscale image without transparency to a coloured image.</p>
  
  <p>This always worked fine until Apple released iOS 10 beta 3. Suddenly the mask is not applied anymore resulting in just a square box being returned with te given color.</p>
  
  <p>The logic behind this can be found at
  <a href="https://developer.apple.com/library/mac/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_images/dq_images.html" rel="nofollow noreferrer">https://developer.apple.com/library/mac/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_images/dq_images.html</a> </p>
  
  <p>under the header <em>Masking an Image with an Image Mask</em></p>
  
  <p>The logic of this code:
  * Take an grayscale image without alpha
  * Create an solid image with the given color
  * Create a mask from the given image
  * Mask the solid image with the created mask
  * Output is a masked image with also respect for colors in between (gray might be light red i.e.).</p>
  
  <p>Has anyone an idea how to fix this function?
  If you have XCode 8 beta 3 you can run this code and on a simulator lower than iOS 10 this will work correct and on iOS 10 it will just create a square box</p>
  
  <p>Example image:
  <a href="https://i.stack.imgur.com/i6IZO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/i6IZO.png" alt="Example image"></a></p>
  
  <pre><code>    public static func image(maskedWith color: UIColor, imageNamed imageName: String) -&gt; UIImage? {
  
      guard let image = UIImage(named: imageName)?.withRenderingMode(.alwaysOriginal) else {
          return nil
      }
  
      guard image.size != CGSize.zero else {
          return nil
      }
  
      guard
          let maskRef = image.cgImage,
          let colorImage = self.image(with: color, size: image.size),
          let cgColorImage = colorImage.cgImage,
          let dataProvider = maskRef.dataProvider
          else {
          return nil
      }
  
      guard
          let mask = CGImage(maskWidth: maskRef.width, height: maskRef.height, bitsPerComponent: maskRef.bitsPerComponent, bitsPerPixel: maskRef.bitsPerPixel, bytesPerRow: maskRef.bytesPerRow, provider: dataProvider, decode: nil, shouldInterpolate: true),
          let masked = cgColorImage.masking(mask)
          else {
          return nil
      }
  
      let result = UIImage(cgImage: masked, scale: UIScreen.main().scale, orientation: image.imageOrientation)
  
      return result
  }
  
  public static func image(with color: UIColor, size: CGSize) -&gt; UIImage? {
  
      guard size != CGSize.zero else {
          return nil
      }
  
      let rect = CGRect(origin: CGPoint.zero, size: size)
  
      UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main().scale)
  
      defer {
          UIGraphicsEndImageContext()
      }
  
      guard let context = UIGraphicsGetCurrentContext() else {
          return nil
      }
  
      context.setFillColor(color.cgColor)
      context.fill(rect)
  
      let image = UIGraphicsGetImageFromCurrentImageContext()
  
      return image;
  }
  </code></pre>