Cancel mask CALayer

enter image description hereI am trying to use CALayer with an image as content to mask a UIView. For the mask, I have a complex png image. If I apply the image as view.layer.mask, I get the opposite behavior of what I want. Is there any way to cancel CAlayer? Here is my code:

layerMask = CALayer()
guard let layerMask = layerMask else { return }    
layerMask.contents = #imageLiteral(resourceName: "mask").cgImage
view.layer.mask = layerMask
// What I would like to to is
view.layer.mask = layerMask.inverse. // <---

I saw several posts about the reverse of CAShapeLayers and Mutable paths, but nothing where I can undo CALayer. What I can do is flip the image in Photoshop so that alpha is inverted, but the problem is that I canโ€™t create the image with the exact size to fit all the screen sizes. Hope this makes sense.

+4
source share
1 answer

, . , . , , , , . CIFilter "Mask To Alpha", .

, . : , , :

enter image description here

, :

enter image description here

, ( , ):

enter image description here

, , :

enter image description here

, , Mask To Alpha CIFilter mask:

enter image description here

, , , . , , , , , , , , .

, - (im) ; :

    let cim = CIImage(image:im)
    let filter = CIFilter(name:"CIMaskToAlpha")!
    filter.setValue(cim, forKey: "inputImage")
    let out = filter.outputImage!
    let cgim = CIContext().createCGImage(out, from: out.extent)
    let lay = CALayer()
    lay.frame = self.iv.bounds
    lay.contents = cgim
    self.iv.layer.mask = lay
+5

Source: https://habr.com/ru/post/1669762/


All Articles