Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 698
Almas Adilbek Avatar asked Jul 16 '12 10:07

Almas Adilbek


1 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;
like image 143
msk Avatar answered Sep 28 '22 05:09

msk