Search code examples
ios6retina-displayquartz-2d

How to draw a one pixel wide UIBezierPath?


I'm running this code in the emulator set to iPhone (Retina 4-inch)

-(void)drawRect:(CGRect)rect {
    [[UIColor whiteColor]set];
    UIRectFill(self.bounds);
    UIBezierPath *vertLine = [UIBezierPath bezierPath];
    [vertLine moveToPoint:CGPointMake(20.5, 10.0)];
    [vertLine addLineToPoint:CGPointMake(20.5, 20.0)];
    vertLine.lineWidth = 1.0;
    [[UIColor blackColor]set];
    [vertLine stroke];
}

I expect to have a one pixel vertical line, but the line the two pixel wide, and solid black. The solid black color seems to suggest that this is not due to a problem with the alignment of the line on the pixel grid (it would be 50% gray in this case)

What's happening here ?


Solution

  • For UIKit you don't specify pixels but points. Points are then mapped automatically to pixels. Your 1-point-width-line seems to be mapped to two retina-pixels.

    See "Points versus Pixels" in http://developer.apple.com/library/ios/#documentation/2DDrawing/Conceptual/DrawingPrintingiOS/GraphicsDrawingOverview/GraphicsDrawingOverview.html