在 ActionScript 3 中,我们可以将掩码应用于这样的可视对象:
SomeVisualObject.mask = maskShapeObject;
如何在 Objective-C 中获得类似的结果?假设我有两个UIImageView
个对象,我想要这样的东西:
imageView1.mask = imageView2;
如何使用一个UIImageView
来掩盖或剪裁另一个的形状?
答案 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;