我试图使 UIImageView 的周围区域变暗,并单独留下一部分(我用蒙版定义的部分)。
现在我正在定义我的 mask 并设置我的 imageView.layer.mask,但是它没有使图像的其余部分变暗,而是完全将其移除。
我想要的效果类型示例:
我得到的示例:
引用文档提到 mask 使用其图层的 alpha,因此我尝试操纵 mask 的不透明度。然而,这似乎只影响了我想单独留下的部分的不透明度,而图像的其余部分仍然被完全切掉。
谁能指出我做错了什么?谢谢。
这是我的代码:
CAShapeLayer *mask = [CAShapeLayer layer];
GMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, nil, 1052, 448);
CGPathAddLineToPoint(path, nil, 2, 484);
CGPathAddLineToPoint(path, nil, 54, 1263);
CGPathAddLineToPoint(path, nil, 56, 1305);
CGPathAddLineToPoint(path, nil, 380, 1304);
CGPathAddLineToPoint(path, nil, 1050, 1311);
CGPathCloseSubpath(path);
mask.path = path;
CGPathRelease(path);
//mask.opacity = 0.5; //doesn't affect the surrounding portion, only the cut out area.
self.imageView.layer.mask = mask;
最佳答案
您做错的是首先使用图层蒙版。您正在尝试使图像的某个区域变暗或变暗。这根本不是图层蒙版的作用!基本上,图层蒙版穿透现有图层,使其背后 显示出来。这正是您发现的:
it's completely removing it instead
是的,因为这就是图层蒙版的作用!如果您不想这样,为什么要使用图层蒙版?
您想要的只是在第一个 ImageView 上放置第二个 ImageView (或只是一个子层)。它包含您绘制的图像。它是透明的,除非它有半透明的深色填充。这将使它背后的东西变暗。您使用剪切路径 来定义未获得深色填充的区域。
或者,通过使用合成或可能使用 CIFilter 在图像顶部绘制来更改 ImageView 中的图像。
关于ios - 向 CALayer 添加半透明蒙版?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15802756/