在我创建以像素为中心的图像编辑器的实验中,我一直在尝试绘制精确的网格叠加,以帮助引导用户尝试访问某些像素。但是,我绘制的网格不是很均匀,尤其是尺寸较小的网格。对于每几个普通列,这是一个略大的列的常规模式,所以我认为这是一个舍入问题,但我在代码中看不到它。这是我的代码:
- (void)drawRect:(NSRect)dirtyRect
{
context = [[NSGraphicsContext currentContext] graphicsPort];
CGContextAddRect(context, NSRectToCGRect(self.bounds));
CGContextSetRGBStrokeColor(context, 1.0f, 0.0f, 0.0f, 1.0f);
CGContextStrokePath(context);
CGContextSetInterpolationQuality(context, kCGInterpolationNone);
CGContextSetShouldAntialias(context, NO);
if (image)
{
NSRect imageRect = NSZeroRect;
imageRect.size = CGImageGetSize([image CGImage]);
drawRect = [self bounds];
NSRect viewRect = drawRect;
CGFloat aspectRatio = imageRect.size.width / imageRect.size.height;
if (viewRect.size.width / viewRect.size.height <= aspectRatio)
{
drawRect.size.width = viewRect.size.width;
drawRect.size.height = imageRect.size.height * (viewRect.size.width / imageRect.size.width);
}
else
{
drawRect.size.height = viewRect.size.height;
drawRect.size.width = imageRect.size.width * (viewRect.size.height / imageRect.size.height);
}
drawRect.origin.x += (viewRect.size.width - drawRect.size.width) / 2.0;
drawRect.origin.y += (viewRect.size.height - drawRect.size.height) / 2.0;
CGContextDrawImage(context, drawRect, [image CGImage]);
if (showPixelGrid)
{
//Draw grid by creating start and end points for vertical and horizontal lines.
//FIXME: Grid is uneven, especially at smaller sizes.
CGContextSetStrokeColorWithColor(context, CGColorGetConstantColor(kCGColorBlack));
CGContextAddRect(context, drawRect);
CGContextStrokePath(context);
NSUInteger numXPoints = (NSUInteger)imageRect.size.width * 2;
NSUInteger numYPoints = (NSUInteger)imageRect.size.height * 2;
CGPoint xPoints[numXPoints];
CGPoint yPoints[numYPoints];
CGPoint startPoint;
CGPoint endPoint;
CGFloat widthRatio = drawRect.size.width / imageRect.size.width;
CGFloat heightRatio = drawRect.size.height / imageRect.size.height;
startPoint.x = drawRect.origin.x;
startPoint.y = drawRect.origin.y;
endPoint.x = drawRect.origin.x;
endPoint.y = drawRect.size.height + drawRect.origin.y;
for (NSUInteger i = 0; i < numXPoints; i += 2)
{
startPoint.x += widthRatio;
endPoint.x += widthRatio;
xPoints[i] = startPoint;
xPoints[i + 1] = endPoint;
}
startPoint.x = drawRect.origin.x;
startPoint.y = drawRect.origin.y;
endPoint.x = drawRect.size.width + drawRect.origin.x;
endPoint.y = drawRect.origin.y;
for (NSUInteger i = 0; i < numYPoints; i += 2)
{
startPoint.y += heightRatio;
endPoint.y += heightRatio;
yPoints[i] = startPoint;
yPoints[i + 1] = endPoint;
}
CGContextStrokeLineSegments(context, xPoints, numXPoints);
CGContextStrokeLineSegments(context, yPoints, numYPoints);
}
}
}
有什么想法吗?
答案 0 :(得分:7)
更新:我设法让你的代码运行一些调整 - CGImageGetSize()来自哪里? - 我无法真正看到问题,除了列甚至不是非常小的尺寸。这就是它必须如何工作。解决这个问题的唯一方法是将缩放比例固定为图像大小的整数倍 - 换句话说,获取小于视图大小的图像大小的最大整数倍 - 或者减少屏幕上绘制的线条数量小尺寸摆脱这个人工制品。只有在大多数编辑器中放大时,像素网格才会变得可见。更不用说如果网格仍然以3-4倍的分辨率可见,那么你就会让视图太忙了。
我无法运行你提供的代码,因为那里有一堆类ivars,但从粗略的一瞥,我会说它与绘制像素边界有关。在你舍入到一个整数以消除模糊的AA人工制品之后(我注意到你关闭了AA,但理想情况下你不应该这样做),然后你需要在你的原点上添加0.5以获得你的线条<像素的强>中心,而不是边界。
像这样:
+---X---+---+---+---+---+
| | | | Y | | |
+---+---+---+---+---+---+
X : CGPoint (1, 1)
Y : CGPoint (3.5, 0.5)
您想要从像素的中心进行绘制,因为否则您的线条会跨越两个像素。
换句话说,在你设置xPoints和yPoints的地方,确保你的值为floor()或round(),然后加0.5。