I am working on image processing mobile app with segmentation technics. I try to get masked image and succeed it. However when I want to save the masked image to my photo album original image is saved even though UIImage view shows the masked image. In addition I wanted change white background pixel colors of the masked area and realized that original image RGB values for masked pixels are still protected. Below is my code to mask the image and the RGB values I read. Masked area pixels are 255 (means 1) non masked area is 0 in the mask image. Please see images I got as expected. But RGB colors and saved image is not masked image but original image (image). What did I make it wrong? Any explanation why are the RGB colors of original image still read in masked image whereas the UIview seems correct (white background)
func maskImage(image:UIImage, mask:UIImage)->UIImage{
let imageReference = image.cgImage
let maskReference = mask.cgImage
let imageMask = CGImage(maskWidth: maskReference!.width,
height: maskReference!.height,
bitsPerComponent: maskReference!.bitsPerComponent,
bitsPerPixel: maskReference!.bitsPerPixel,
bytesPerRow: maskReference!.bytesPerRow,
provider: maskReference!.dataProvider!, decode: nil, shouldInterpolate: true)
let maskedReference = imageReference!.masking(imageMask!)
let maskedImage = UIImage(cgImage:maskedReference!)
print(mask.getPixelColor(x: 0, y: 0)!)
print(image.getPixelColor(x: 0, y: 0)!)
print(maskedImage.getPixelColor(x: 0, y: 0)!)
return maskedImage
}
printed values for the first pixel (left top) are in sequence: UIExtendedSRGBColorSpace 1 1 1 1 (mask) white as expected UIExtendedSRGBColorSpace 0.0313725 0.0313725 0.0313725 1 (image) almost black as expected UIExtendedSRGBColorSpace 0.0313725 0.0313725 0.0313725 1 (masked image) suppose to be 1 1 1 1