在Objective-C中屏蔽视图

时间:2012-07-16 10:32:46

标签: ios objective-c uiimageview mask

ActionScript 3 中,我们可以将掩码应用于这样的可视对象:

SomeVisualObject.mask = maskShapeObject; 

如何在 Objective-C 中获得类似的结果?假设我有两个UIImageView个对象,我想要这样的东西:

imageView1.mask = imageView2;

如何使用一个UIImageView来掩盖或剪裁另一个的形状?

1 个答案:

答案 0 :(得分:6)

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;