简体   繁体   中英

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

I have UIImageView with added mask to it's layer. 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. You can then give that second layer the same bounds, corner radius and border as _photo 's layer without the mask applying to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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