Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 585
BenL0 Avatar asked Nov 19 '25 00:11

BenL0


1 Answers

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.

like image 111
Jakob Egger Avatar answered Nov 21 '25 08:11

Jakob Egger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!