在ActionScript 3中,我们可以将掩码应用于可视对象,如下所示:
SomeVisualObject.mask = maskShapeObject;
如何在Objective-C中实现类似的结果?假设我有两个UIImageView对象,我想要这样的东西:
imageView1.mask = imageView2;
如何使用一个UIImageView来屏蔽或剪裁另一个的形状?
解决方法
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,1024,768)]; imageViewToMask.image = [UIImage imageNamed:@"image2.png"]; imageViewToMask.layer.mask = maskLayer;