Adding a mask with CALayers

后端 未结 2 505
清酒与你
清酒与你 2021-02-07 21:51

I seem to be having difficulties adding a mask via CALayers. I\'m simply trying to mask a UIImageView. Here\'s my code:

 CALayer *maskLayer = [CALayer layer];
 U         


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-07 22:21

    try this:

    CALayer *maskLayer = [CALayer layer];
    UIImage *mask = [UIImage imageNamed:@"mask.png"];
    maskLayer.contents = (id)mask.CGImage;
    //  maskLayer.contentsGravity = kCAGravityCenter;
    maskLayer.frame = CGRectMake(0.0, 0.0,1024,768);
    
    UIImageView *viewToMask = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
    viewToMask.image = [UIImage imageNamed:@"picture.png"];
    viewToMask.layer.mask = maskLayer;
    [self.view addSubview:viewToMask];
    

    you also need to set mask frame

提交回复
热议问题