levo4ka
levo4ka

Reputation: 2298

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: enter image description here Result is next: enter image description here

How I can add border without changing it's color during mask apply?

Upvotes: 1

Views: 272

Answers (1)

Robert
Robert

Reputation: 5859

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.

Upvotes: 2

Related Questions