KeroppiMomo
KeroppiMomo

Reputation: 594

CGImage Masking with UIImage pngData()

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://i.sstatic.net/pK8fK.jpg (then click the 'Export...' button and save as png image)

How can I really mask an image?

Upvotes: 4

Views: 1172

Answers (1)

Andrew Romanov
Andrew Romanov

Reputation: 5076

Just draw a masked image into image context:

func drawImage(_ image: UIImage) -> UIImage?
    {
        guard let coreImage = image.cgImage else {
            return nil;
        }
        UIGraphicsBeginImageContext(CGSize(width: coreImage.width, height: coreImage.height))
        image.draw(in: CGRect(x: 0.0, y: 0.0, width: coreImage.width, height: coreImage.height))
        let resultImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return resultImage;
    }

Also you can draw image into bitmap context or image renderer and get result image.

Upvotes: 6

Related Questions