How do I properly use CGImage.masking(_ mask:CGImage)->CGImage? ?

On iOS, I need to use an image resource as a mask to create a tinted image. I cannot use CALayer or UIView tinting or masking capacities.

I'm trying to use CGImage.masking(_ mask:CGImage)->CGImage?

however, with the folowing code in an iOS Swift Playground, all I get from the mask is the same square solid-color image.

I have already tried changing the "maskUiImage" from using a black image with an alpha mask to both a white image on black background with no alpha and a white image on a black background, none of these change affected the output of the final image, though they do affect the output of "uiMask".


import UIKit

import CoreGraphics

extension CGImage {

public class func make(size:CGSize, scale:CGFloat, drawing:(CGContext)->())->CGImage {

UIGraphicsBeginImageContextWithOptions(size, false, scale)

let bitmapContext:CGContext = UIGraphicsGetCurrentContext()!

drawing(bitmapContext)

let image = bitmapContext.makeImage()!

UIGraphicsEndImageContext()

return image

}

}

CGBitmapInfo.alphaInfoMask.rawValue

let maskUiImage:UIImage = #imageLiteral(resourceName: "si_bolt_solid_opaque_black@3x.png")//provide your own masking image literal

let maskImage:CGImage = maskUiImage.cgImage!

let imageMask:CGImage = CGImage(maskWidth: maskImage.width, height: maskImage.height, bitsPerComponent: maskImage.bitsPerComponent, bitsPerPixel: maskImage.bitsPerPixel, bytesPerRow: maskImage.bytesPerRow, provider: maskImage.dataProvider!, decode: nil, shouldInterpolate: false)!

let uiMask = UIImage(cgImage:imageMask, scale:maskUiImage.scale, orientation:.up)

let unmaskedImage:CGImage = CGImage.make(size: maskUiImage.size, scale: maskUiImage.scale) { (context) in

context.setFillColor(UIColor.green.cgColor)

context.fill(CGRect(origin:.zero, size:maskUiImage.size))

}

let uiUnmaskedImage = UIImage(cgImage: unmaskedImage, scale:maskUiImage.scale, orientation:.up)

guard let maskedImage:CGImage = unmaskedImage.masking(imageMask) else {

fatalError("new image was nil")

}

let uiMaskedImage:UIImage = UIImage(cgImage:maskedImage, scale:maskUiImage.scale, orientation:.up)


//the problem is that "uiUnmaskedImage" and "uiMaskedImage" are identical. "uiUnmaskedImage" is as expected, a green square the size of my mask image. However, unexpectly, and undesirably, "uiMaskedImage" is also a green square. I want the masked image to be transparent where I don't provide the passthrough-color in the original, and my color selected in the "CGImage.make(size:..." function where I do provide the passthrough color in the original masking image. I can ask our designer to configure the original mask image anyway it needs to be, but I've already tried several combinations with no impact on the final result.

In Obj-C, the .masking(_:) function is known as "

CGImageRef CGImageCreateWithMask(CGImageRef image, CGImageRef mask)".
How do I properly use CGImage.masking(_ mask:CGImage)->CGImage? ?
 
 
Q