Mask a view in Objective-C

In ActionScript 3 we can apply a mask of to a visual object like this:

SomeVisualObject.mask = maskShapeObject; 

How can I achieve similar result in Objective-C? Assume I have two UIImageView objects, I want something like this:

imageView1.mask = imageView2;

How can I use one UIImageView to mask, or clip the shape of, another?

Answers


CALayer *maskLayer = [CALayer layer];
 UIImage *maskImage = [UIImage imageNamed:@"maskImage.png"];
maskLayer.contents = (id)maskImage.CGImage;
maskLayer.bounds = (CGRect){CGPointZero, maskImage.size};

UIImageView *imageViewToMask = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
imageViewToMask.image = [UIImage imageNamed:@"image2.png"];
imageViewToMask.layer.mask = maskLayer;

Need Your Help

TFS Build Notifications not showing any projects

visual-studio visual-studio-2013 tfs azure-devops

I have multiple team projects in TFS (Visual Studio Online), each having 1 or more build definitions (non-XAML). I'd like to use the Build Notifications tray app to monitor these builds, however no...

Convert from hexadecimal to binary C++

c++ binary hex

This kind of builds up on Already asked question...