Mask a view in Objective-C

岁酱吖の 提交于 2019-12-13 12:06:12

问题


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?


回答1:


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;


来源:https://stackoverflow.com/questions/11502539/mask-a-view-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!