kCGBlendModeClear 不清除,为什么?
我正在编写几行代码来熟悉 Quartz 2d 的基础知识。 我正在尝试绘制和图像,然后通过 kCGBlendModeClear 混合模式清除它。这是我的 UIView 子类代码的代码,其背景颜色通过 IB 设置为橙色:
- (void)drawRect:(CGRect)rect {
UIImage *brush = [UIImage imageNamed:@"brush.png"] ;
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetBlendMode(ctx,kCGBlendModeNormal );
CGContextDrawImage(ctx, CGRectMake(100, 100, 26, 25), [brush CGImage]);
CGContextSetBlendMode(ctx, kCGBlendModeClear);
CGContextSetFillColorWithColor(ctx, [UIColor clearColor].CGColor);
CGContextFillEllipseInRect(ctx, CGRectMake(110, 110, 5, 5)); // HERE!
}
阅读文档和 这个问题 我认为标记为此处的线会在图像中产生一个洞之前画过。相反,它会在其上创建一个黑色圆圈(应该是橙色)。
为了调试,我尝试在橙色 uiview 上添加自定义视图。这次我的自定义视图有黑色背景。这里的线孔是正确的,但我想知道为什么视图是黑色的。更奇怪的是,如果我执行 myView.backgroundColor 我可以设置背景颜色(这不应该被我的 drawRect 实现覆盖吗?)。
我显然缺少 Quartz 的一些基础知识,有人可以帮忙吗?
达维德
I'm writing some lines of code to get acquainted with the basics of Quartz 2d.
I am trying to draw and image an then clear it through the kCGBlendModeClear blend mode. Here's the code of my UIView
subclass code, whose background color is set to orange through IB:
- (void)drawRect:(CGRect)rect {
UIImage *brush = [UIImage imageNamed:@"brush.png"] ;
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetBlendMode(ctx,kCGBlendModeNormal );
CGContextDrawImage(ctx, CGRectMake(100, 100, 26, 25), [brush CGImage]);
CGContextSetBlendMode(ctx, kCGBlendModeClear);
CGContextSetFillColorWithColor(ctx, [UIColor clearColor].CGColor);
CGContextFillEllipseInRect(ctx, CGRectMake(110, 110, 5, 5)); // HERE!
}
Reading the docs and this question I thought that line marked HERE would produce a hole in the image I had previously drawn. Instead it creates a black circle on it (should be orange).
To debug, I tried adding my custom view over an orange uiview. This time my custom view has a black background. The hole of line HERE is correct, but I wonder why the black color of the view. Even more strangely, if I do myView.backgroundColor I can set a background color (shouldn't this be overridden by my drawRect implementation?).
I am clearly missing some basics of Quartz, can anyone help?
Davide
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有几件事。
首先,Porter-Duff 混合模式只能保证在基于位图的上下文中工作。 (即由
CGBitmapContextCreate
创建的上下文)其次,
kCGBlendModeClear
被定义为R = 0
— 它甚至不检查 src 或 dst 像素。因此,尽管不能保证它在这里工作,但它似乎工作正常。(请参阅 CGContext.h 以获得比文档给出的更好的解释。)
Couple of things.
First, the Porter-Duff blend modes are only guaranteed to work in bitmap-based contexts. (i.e., contexts created by
CGBitmapContextCreate
)Second,
kCGBlendModeClear
is defined asR = 0
— it doesn't even check src or dst pixels. So even though it's not guaranteed to work here, it appears that it is working correctly.(see
CGContext.h
for better explanation than the docs give.)为了使 kCGBlendModeClear 正常工作,视图不能是不透明的,并且不能设置背景颜色。
另外,下面这行是不必要的,因为 kCGBlendModeClear 不关心填充颜色:
For
kCGBlendModeClear
to work the view must not be opaque and must not have a background color set.In addition, the following line is unnecessary, because
kCGBlendModeClear
does not care about the fill color: