How to draw a shape on top of a UIImage while respecting the image's alpha mask

前端 未结 4 1619
隐瞒了意图╮
隐瞒了意图╮ 2021-01-31 12:29

I need a UIImageView that can draw itself in color or b/w according to a flag:

  BOOL isGrey;

I\'m trying to do it by drawing a black rectangle

4条回答
  •  故里飘歌
    2021-01-31 13:18

    You could try to add a mask to the view's Core Animation layer.

    CALayer *maskLayer = [CALayer layer];
    [maskLayer setBounds:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];
    
    // Center the layer
    [maskLayer setPosition:CGPointMake([self view].bounds.size.width/2, 
                              [self view].bounds.size.height/2)];
    
    // Set the cornerRadius to half the width/height to get a circle.
    [maskLayer setCornerRadius:50.f];
    [maskLayer setMasksToBounds:YES];
    
    // Any solid color will do. Just using black here.
    [maskLayer setBackgroundColor:[[UIColor blackColor] CGColor]];
    
    // Set the mask which will only allow that which is in the
    // circle show through
    [[[self view] layer] setMask:maskLayer];
    

    One other thought. Why can't you just create a b&w version of the image and swap that out depending on your flag?

提交回复
热议问题