开发者

How to get pixel perfect drawing with cocoa

I'm trying to draw some of my UI elements in Cocoa, mainly icons for buttons, but I'm having great difficulty getting the kind of precision I'd like开发者_如何学编程.

I'm using super simple code like this to draw rectangles:

[[NSColor redColor] set];
[NSBezierPath strokeRect:myRect];

But what I'm seeing is the red rectangle line is always faded.

What am I missing here?


The Cocoa coordinates actually specify the center of the pixel you want to draw. This means, for example, if you want to draw on the bottom left pixel, you should use the coordinates (0.5,0.5).

Add/Subtract half a pixel to your coordinates and they should be pixel-perfect.


You can disable antialiasing for your graphics context like this:

[[NSGraphicsContext currentContext] setShouldAntialias:NO];

Or you could use NSFrameRect instead of a bezier path, and that would get the precision you want, while keeping antialiasing on:

NSFrameRect(myRect);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜