问题描述
我正在尝试使用带有图像的CALayer作为屏蔽UIView的内容。对于面具我有复杂的png图像。如果我将图像应用为view.layer.mask,我会得到与我想要的相反的行为。
有没有办法扭转CAlayer?这是我的代码:
I am trying to use a CALayer with an image as contents for masking a UIView. For the mask I have complex png image. If I apply the image as a view.layer.mask I get the opposite behaviour of what I want.Is there a way to reverse the 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. // <---
我在反向CAShapeLayers和Mutable路径上看过几个帖子,但没有在哪里我可以反转CALayer。
我能做的是在Photoshop中反转图像以便反转alpha,但问题是我无法创建具有适合所有屏幕尺寸的精确尺寸的图像。我希望它确实有意义。
I have seen several posts on reverse CAShapeLayers and Mutable paths, but nothing where I can reverse a CALayer.What I could do is reverse the image in Photoshop so that the alpha is inverted, but the problem with that is that I won't be able to create an image with the exact size to fit all screen sizes. I hope it does make sense.
推荐答案
我要做的是实时构建掩码。如果您有徽标的黑色图像,这很容易。使用标准技术,您可以将徽标图像绘制为实时构建的图像,以便您负责图像的大小以及图标的大小和位置。使用Mask To AlphaCIFilter,您可以将黑色转换为透明以用作图层蒙版。
What I would do is construct the mask in real time. This is easy if you have a black image of the logo. Using standard techniques, you can draw the logo image into an image that you construct in real time, so that you are in charge of the size of the image and the size and placement of logo within it. Using a "Mask To Alpha" CIFilter, you can then convert the black to transparent for use as a layer mask.
所以,举例说明。这是背景图片:这是我们想要在前台打孔的地方看到的:
So, to illustrate. Here's the background image: this is what we want to see wherever we punch a hole in the foreground:
这是前景图片,位于背景之上并完全隐藏它:
Here's the foreground image, lying on top of the background and completely hiding it:
这是徽标,黑色(忽略灰色,代表透明度):
Here's the logo, in black (ignore the grey, which represents transparency):
这是在代码中绘制的标识为正确尺寸的白色背景:
Here's the logo drawn in code into a white background of the correct size:
最后,这是相同的图像转换为Mask To Alpha CIFilter的掩码,并附加到前景图像视图作为其掩码
:
And finally, here's that same image converted into a mask with the Mask To Alpha CIFilter and attached to the foreground image view as its mask
:
好的,我本可以更好地选择我的图像,但是这个就是我躺在那里。您可以看到,只要徽标中有黑色,我们就会在前景图像上打一个洞并看到背景图像,我相信这正是您所说的想要做的。
Okay, I could have chosen my images a little better, but this is what I had lying around. You can see that wherever there was black in the logo, we are punching a hole in the foreground image and seeing the background image, which I believe is exactly what you said you wanted to do.
关键步骤是最后一步,即将徽标的黑白图像( im
)转换为蒙版;这是我如何做到的:
The key step is the last one, namely the conversion of the black-on-white image of the logo (im
) to a mask; here's how I did that:
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
这篇关于反转CALayer面具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!