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

前端 未结 4 1618
隐瞒了意图╮
隐瞒了意图╮ 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:06

    Although it doesn't directly answer your question, you may be able to get the effect you are looking for by using the kCGBlendModeLuminosity blend mode:

    - (void)drawRect:(CGRect)rect {
        CGSize imageSize = [image size];
        CGContextRef c = UIGraphicsGetCurrentContext();
        if (isGrey)
            CGContextSetBlendMode(c, kCGBlendModeLuminosity);
        CGContextScaleCTM(c, 1.0, -1.0);
        CGContextDrawImage(c, CGRectMake(0.0f, 0.0f, imageSize.width, -imageSize.height), [image CGImage]);
    }
    

    Also, the rect passed to drawRect: is the invalid area, not the entire area. You should be using the bounds instead

提交回复
热议问题