我正在使用CGMutablePathRef创建任意多边形,所以我可以使用CGContextClip在我的上下文中进一步绘制图形。我怎样才能让CGContextClip包含边界像素?
这具有非包含路径的“边界”像素的非常烦人和不希望的行为。因此,例如,在这里我的代码片段,我剪辑了一半的矩形,然后着色各占一半 - 生成的图像留下一条细线,其中的路径的边框不着色:
- (void)drawRect:(CGRect)rect
{
CGContextRef ctxt = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(ctxt, [UIColor whiteColor].CGColor);
CGContextFillRect(ctxt, rect);
CGContextSaveGState(ctxt);
//Create a path to clip a triangle:
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 0, 0);
CGPathAddLineToPoint(path, NULL, rect.size.width, 0);
CGPathAddLineToPoint(path, NULL, 0, rect.size.height);
CGContextAddPath(ctxt, path);
CGContextClip(ctxt);
//Fill the triangle with dark gray:
CGContextSetFillColorWithColor(ctxt, [UIColor darkGrayColor].CGColor);
CGContextFillRect(ctxt, rect);
CGPathRelease(path);
CGContextRestoreGState(ctxt);
//Create a path to clip a triangle:
path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 0, rect.size.height);
CGPathAddLineToPoint(path, NULL, rect.size.width, 0);
CGPathAddLineToPoint(path, NULL, rect.size.width, rect.size.height);
CGContextAddPath(ctxt, path);
CGContextClip(ctxt);
//Fill the triangle with dark Gray:
CGContextSetFillColorWithColor(ctxt, [UIColor darkGrayColor].CGColor);
CGContextFillRect(ctxt, rect);
CGPathRelease(path);
}
如何以包含“边框”像素的方式剪裁上下文?
所以你说,像素0,0的位置应该被引用为(-0.5,-0.5)? – typewriter 2013-03-06 06:44:39
它应该被引用为(0.5,0.5),因为像素“at”(0,0)是从(0,0)到(1,1)的矩形。 – Tony 2013-03-06 15:27:01
我明白你在说什么。而且,为了让所有的绘图都在像素上,可以简单地将(0.5,0.5)加到每个坐标上吗?这确实使得给出的例子看起来是正确的。但是,在实践中,我不是绘制矩形,而是绘制复杂的多边形。在这种情况下,这种简单的转变并不能解决问题,因此必须有更多的转变。 – typewriter 2013-03-06 16:47:36