I'm trying to mask an image in swift. Here's my code:
(Normally, originalImg is an image that is loaded from UIImagePickerController and then cropped by UIGraphicsGetImageFromCurrentImageContext(), and
makedImage is an image that is drawn by the user, created by UIGraphicsGetImageFromCurrentImageContext())
func imageMasking(_ originalImg: UIImage, maskImage: UIImage) -> UIImage {
let cgMaskImage = maskImage.cgImage!
let mask = CGImage(maskWidth: cgMaskImage.width, height: cgMaskImage.height, bitsPerComponent: cgMaskImage.bitsPerComponent, bitsPerPixel: cgMaskImage.bitsPerPixel, bytesPerRow: cgMaskImage.bytesPerRow, provider: cgMaskImage.dataProvider!, decode: nil, shouldInterpolate: true)!
return UIImage(cgImage: originalImg.cgImage!.masking(mask)!)
}
When I display the resulted UIImage to UIImageView, it works well. However, when I try to get the pngData() of the resulted UIImage, the image data is identical to originalImg.
I also tried to export the pngData() of the resulted image from Xcode, but it is still the same as originalImg.
Image: https://imgur.com/tEVBWUQ (then click the 'Export...' button and save as png image)
How can I really mask an image?