//CALayer 蒙版图层真正厉害的地方在于蒙版图层不局限于静态图。任何有图层构成的都可以作为mask属性,这意味着你的蒙版可以通过代码甚至是动画实时生成!
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,strong) UIImageView *layerView1;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_layerView1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
_layerView1.backgroundColor = [UIColor whiteColor];
self.layerView1.layer.shadowOpacity = 0.5f;
//给ImageView添加图像
_layerView1.image = [UIImage imageNamed:@"2"];
//创建maskLayer
CALayer *maskLayer = [CALayer layer];
//给maskLayer 设定frame
maskLayer.frame = self.layerView1.bounds;
UIImage *maskImage = [UIImage imageNamed:@"1"];
//给图层的contents添加内容
maskLayer.contents = (__bridge id)maskImage.CGImage;
self.layerView1.layer.mask = maskLayer;
[self.view addSubview:_layerView1];
}