Search code examples
iphoneiosxcodecoordinatesretina-display

Drawing, using actual pixels, on retina display


What I need to do to draw some shapes on retina display, defining actual screen pixels. I mean that I need that all lines will be 1 pixel width, and no antialiasing. And, if i write the the code:

CGContextMoveToPoint(context,0,0);
CGContextAddLineToPoint(context,0,959);

then on display will be line from the upper left most pixel toward lowest left most pixel including it.

If I understood well, I need to use CGContextScaleCTM. But there is not always the same regularity in coordinates. Sometimes there is offset on Y-axis, and sometimes no. I have tried to write some macro for converting the coordinates, but have confused at all. I know, that there is logical points and auto positioning stuff. But that is the point that I need to draw all the pixels manually, cause I'm developing some util.


Solution

  • Try this instead:

    CGContextMoveToPoint(context, 0.5, 0);
    CGContextAddLineToPoint(context, 0.5, 959);
    

    Why: A 1.0 pixel wide line will be centered on the coordinates you give it. So if you start at (0, 0), half of the line will be in the column from -1 to 0, and half of the line will be in the column from 0 to 1. Integer coordinates give you the corners of the pixels, which is good for things like raster images.