-1

I want to create a layer to act as a mask for my UIImageView which was created in interface builder. However, no matter what I do the layer remains white. The code is pretty straightforward any ideas what is causing this behavior ?

UIImage* image = [UIImage imageNamed:@"face"];
self.imageView.image = image;

CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
maskLayer.frame = self.imageView.bounds;
maskLayer.fillColor = [[UIColor blackColor] CGColor];
maskLayer.path = CGPathCreateWithRect(self.imageView.bounds, NULL);

self.imageView.layer.mask = maskLayer;
self.maskLayer = maskLayer;

I edited the code and added the path but it still does not work.

Joel Parker
  • 295
  • 1
  • 4
  • 17
  • This is not a duplicate, my code is essentially the same as that other post except I used the UIImageView in interface builder. With one big difference, unlike the other post ... mine does not work and I am trying to find out why – Joel Parker Mar 31 '17 at 23:06
  • Your question as originally asked did not set the path. After I marked it as duplicate, you edited to add a path. It is now a different question - reopenned. – foundry Apr 01 '17 at 01:30
  • What result are you expecting to achieve? "It still does not work" is not an explanation. What you are doing seems an odd thing to do so I cannot guess what you _mean_ to do. – matt Apr 01 '17 at 01:38
  • I am trying to achieve the exact effect that Rob provided the code for in this post: http://stackoverflow.com/questions/20165906/how-to-crop-image-inside-the-circle-in-uiimageview-in-ios but instead of a white mask I wanted to make a black translucent color. For some reason everything I try does not change the white mask layer to a color other than white. I was trying to simplify this with the code above to just create a colored mask so I can get an idea of how to do it before integrating it into Rob's code. – Joel Parker Apr 01 '17 at 01:59
  • "but instead of a white mask I wanted to make a black translucent color" That makes no sense. A mask has no color. It simply punches a hole in a view, to a greater or lesser extent. There is no such thing as a "colored mask". — This may be what we call an x-y problem: instead of asking how to tint an image view, you have assumed you know what a mask is and that a mask can do this, and have asked a question about a mask. I have answered, describing what I take to be your misunderstanding of the matter. – matt Apr 01 '17 at 02:06

2 Answers2

1

You seem to have a misconception as to what a mask is. It is a set of instructions, in effect, for injecting transparency into a view, thus "punching a hole" through the view to a greater or lesser extent (depending on the degree of transparency). It has no color. You are seeing white because that is the color of what is behind your image view — you have punched a hole through the entire image view and made it invisible.

matt
  • 515,959
  • 87
  • 875
  • 1,141
0

From the docs:

The layer’s alpha channel determines how much of the layer’s content and background shows through. Fully or partially opaque pixels allow the underlying content to show through but fully transparent pixels block that content.

Black fill (interior of the closed path) = opaque pixels
Area outside of the closed path = transparent pixels
Image will appear on the interior region of your path and not appear outside of that path. The actual colour is irrelevant: when you set a layer as a mask, all we are interested in now is opacity.

The path is ostensively the same size as the imageView, so you won't see any difference as the mask matches the imageView bounds. Additionally , if you use this code before the geometry is fully set, such as in viewDidLoad, you may not get the results you expect.

As matt suggests - you need to think what result you are after.

If you want a "black translucent color" - or similar - consider adding another translucent-colored layer to the mix. But this is not a mask.

foundry
  • 31,615
  • 9
  • 90
  • 125