CGImage Masking with UIImage pngData()

拥有回忆 提交于 2021-02-10 13:16:09

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/53967126/cgimage-masking-with-uiimage-pngdata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!