掩模在Objective-C的图(Mask a view in Objective-C)

2019-07-30 11:07发布

ActionScript 3我们可以申请到这样的视觉对象的遮罩:

SomeVisualObject.mask = maskShapeObject; 

我怎样才能达到同样的效果在Objective-C? 假设我有两个UIImageView对象,我想是这样的:

imageView1.mask = imageView2;

如何使用一个UIImageView掩盖,或夹,另外的形状?

Answer 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;


文章来源: Mask a view in Objective-C