[英]How to avoid color blending while drawing rectangles?
While drawing rectangles in a UIView for ios I keep having the same issue: the rectangle margins blend their color with the background. 在UIView for ios中绘制矩形时,我仍然遇到同样的问题:矩形边距将它们的颜色与背景混合。 I tried several rectangle drawing methods with the same results — using a bezier path, drawing line by line, drawing the margins and filling up the insides of the rectangle.
我尝试了几种具有相同结果的矩形绘制方法 - 使用贝塞尔曲线路径,逐行绘制,绘制边距并填充矩形的内部。
The view's frame's themselves are always correctly draw. 视图的框架本身始终正确绘制。 I've even considered using views whenever I need a rectangle, but that doesn't seem like the correct way to do things specially because I need to draw many of them.
我甚至考虑在需要矩形时使用视图,但这似乎不是特别做事的正确方法,因为我需要绘制其中的许多。
Here is an example of my problem. 这是我的问题的一个例子。 For comparison, I'm drawing a rectangular green view with the same dimensions:
为了比较,我正在绘制一个具有相同尺寸的矩形绿色视图:
CGRect horizontalBar = CGRectMake(2, 2, 10, 6);
UIView* horizontalBarSubView = [[UIView alloc] initWithFrame:horizontalBar];
horizontalBarSubView.backgroundColor = [UIColor greenColor];
[self addSubview:horizontalBarSubView];
[horizontalBarSubView release];
And for the rectangle itself: 对于矩形本身:
CGRect horizontalBar = CGRectMake(2, 20, 10, 6);
UIBezierPath* horizontalBarPath = [UIBezierPath bezierPathWithRect:horizontalBar];
[[UIColor greenColor] set];
[horizontalBarPath fill];
This wields the following result (10x zoom): 这将产生以下结果(10倍变焦):
What is causing the colors to blend? 是什么导致颜色混合?
How can I avoid this? 我怎么能避免这个?
Have you tried messing with CGContext? 你有没有试过搞乱CGContext?
CGContextSetShouldAntialias CGContextSetShouldAntialias
Look at Graphics Context Settings here: 在这里查看图形上下文设置:
http://www.apeth.com/iOSBook/ch15.html http://www.apeth.com/iOSBook/ch15.html
"When you draw in a graphics context, the drawing obeys the context's current settings. Thus, the procedure is always to configure the context's settings first, and then draw." “当您在图形上下文中绘制时,绘图将遵循上下文的当前设置。因此,过程始终首先配置上下文的设置,然后绘制。”
You'd want a context that does not have the CGContextSetShouldAntialias flag set. 您需要一个没有设置CGContextSetShouldAntialias标志的上下文。
Though as stated in the comments, this will fill pixels that would otherwise be blended with the rectangle color. 虽然如评论中所述,但这将填充否则将与矩形颜色混合的像素。
@Almo is correct that your problem is antialiasing, but are you sure this is the actual code and values? @Almo是正确的,你的问题是抗锯齿,但你确定这是实际的代码和值吗? This shouldn't anti-alias, and my tests show it doesn't.
这不应该是反别名,我的测试表明它没有。 To get your result, I have to change your path to this:
为了得到你的结果,我必须改变你的道路:
CGRect horizontalBar = CGRectMake(2.5, 20.5, 10, 6);
Filling to a path on a fractional pixel will give you the effect you're seeing. 填充分数像素上的路径将为您提供您所看到的效果。 Also, stroking a path on integral pixels with an odd width will anti-alias this way.
此外,在具有奇数宽度的整数像素上描绘路径将以这种方式消除锯齿。 (All on a non-Retina display; Retina is slightly different.)
(全部采用非Retina显示屏; Retina略有不同。)
I do not generally recommend turning off anti-aliasing as @Almo suggests. 我一般不建议关闭抗锯齿,因为@Almo建议。 Instead, you just need to make sure your paths draw or fill whole pixels.
相反,您只需要确保您的路径绘制或填充整个像素。
This is covered in more detail in Chapter 6 of iOS 5 Programming Pushing the Limits , page 114. iOS 5编程推送限制的第6章,第114页详细介绍了这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.