I just inverted the color of the mask with the help of this function and it is working perfectly.
func invertColors(of ciImage: CIImage) -> CIImage? {
let invertFilter = CIFilter(name: "CIColorInvert")
invertFilter?.setValue(ciImage, forKey: kCIInputImageKey)
return invertFilter?.outputImage
}