简体   繁体   English

如何在Objective-C的蒙版图像上添加没有蒙版的边框

[英]How to add border with no mask to masked image at Objective-C

I have UIImageView with added mask to it's layer. 我有UIImageView及其图层添加蒙版。 And after that I want to add border to the image. 之后,我想为图像添加边框。 But, looks like mask applying to the border also, because it's changing color (should be strong white, w\\o mask). 但是,看起来像蒙版也应用于边框,因为它正在改变颜色(应该是浓白色,没有蒙版)。

My code for doing this looking as 我这样做的代码看起来像

// Mask
CALayer *layer = [CALayer layer];
layer.frame = _photo.bounds;
layer.backgroundColor = [UIColor colorWithWhite:0.f alpha:.5f].CGColor;
[_photo.layer setMask:layer];

_photo.layer.cornerRadius = _photo.frame.size.width / 2;
_photo.layer.masksToBounds = YES;
_photo.clipsToBounds = YES;

// Border
_photo.layer.borderColor = [[UIColor whiteColor] CGColor];
_photo.layer.borderWidth = 2.0;

Image w\\o mask applied: 没有遮罩的图片: 在此处输入图片说明 Result is next: 结果是下一个: 在此处输入图片说明

How I can add border without changing it's color during mask apply? 如何在蒙版应用期间添加边框而不更改其颜色?

I suspect you can't (easily) stop the layer's mask from also masking the layer's border. 我怀疑您无法(​​轻松)停止图层的遮罩来遮盖图层的边界。 That being the case, I'd suggest creating a second view or layer just for the border, in front of _photo but outside its view hierarchy. 既然如此,我建议为边框创建第二个视图或图层,在_photo但在视图层次之外。 You can then give that second layer the same bounds, corner radius and border as _photo 's layer without the mask applying to it. 然后,您可以给第二层与_photo的层相同的边界,拐角半径和边界,而无需应用蒙版。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM