0

I'm trying to create a line of 3 pixels width on a retina @2x display. The simple idea would be to create a 1.5 width line :

UIGraphicsBeginImageContextWithOptions(CGSizeMake(20, 20), NO, 0.0f);
CGContextRef aRef = UIGraphicsGetCurrentContext();
CGContextSetAllowsAntialiasing(aRef, NO);
CGContextSetShouldAntialias(aRef, NO);
UIBezierPath* bezierPath = UIBezierPath.bezierPath;
[bezierPath moveToPoint: CGPointMake(10, 0)];
[bezierPath addLineToPoint: CGPointMake(10, 10)];
bezierPath.lineWidth = 1.5;
[bezierPath stroke];
UIImage * myImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

But at the end, I end up with a 4 pixels line width on screen.

The thing is, I'm using the iPad 3 (so retina @2x), and when I use a UIBarButtonItem with the predefined system button UIBarButtonSystemItemAdd, the two paths of the cross are 3 pixels width on my screen.

2 Answers2

0

I suspect it's b/c your path is 1.5 points, but is started at (0,0). since the path is drawn half on one side of the path, and half on the other, it means that .75 pts are draw above/below the path.

that'd mean your path reaches from (in px):

left:   (-1.5, 0) =>  (-1.5, 10) 
center: (0,0)     =>  (0,10) 
right:  (1.5,0)   =>  (1.5,10)

that means each side will render using 2 pixels.

instead, you probably want to create the line from (.5, 0) => (.5, 10), which would align the path width to the pixels on screen:

left:   (-1, 0) =>  (-1, 10) 
center: (.5, 0) =>  (.5, 10) 
right:  (2,  0) =>  (2,  10)
adam.wulf
  • 2,149
  • 20
  • 27
  • My mistake, the path's supposed to be in the middle of the image. And that still doesn't change anything. If I use a path width of 1.0, it's rendered at 2 pixels on screen. If I use a path width of 2.0, it's rendered at 4 pixels on screen. So a path at 1.5 should be rendered at 3 pixels on screen... – user3048615 Nov 08 '14 at 09:08
  • Right now your path starts and stops between pixels, and instead you need to align your path into the center of te pixels. – adam.wulf Nov 09 '14 at 10:15
0

It worked with :

UIBezierPath* rectanglePath = [UIBezierPath bezierPathWithRect: CGRectMake(13, 21, 18, 1.5)];