質問

I'm trying to draw a simple line, the problem is that it is coming out not as 1 pixel in width but 2. The docs state that user space units will translate to a pixel, if I read it correctly.

The code is as simple as it gets, yet my line is always 2 pixels wide.

//Get the CGContext from this view
CGContextRef context = UIGraphicsGetCurrentContext();
//Set the stroke (pen) color
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);

//Set the width of the pen mark
CGContextSetLineWidth(context, 1);

for (int i = 1; i <= sections - 1; i++)
{
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(0.0, i * kSectionHeight)];

    CGContextMoveToPoint(context, 0.0, i * kSectionHeight); //Start point
    CGContextAddLineToPoint(context, self.frame.size.width, i * kSectionHeight);
}

CGContextStrokePath(context);

enter image description here

役に立ちましたか?

解決

No points don't translate to pixels. If your line is too thick change the value.

他のヒント

I tried the following in a test app:

CGFloat desiredWidthInPixels = 1.0f;
CGFloat lineWidth = desiredWidthInPixels / [[UIScreen mainScreen] scale];
NSLog(@"lineWidth = %f", lineWidth);

I got a lineWidth of 0.5f, which should translate accurately to a single pixel width on the screen (since scale is 2.0, indicating a "Retina" device). This should adapt correctly across devices of different screen sizes.

***Caveat: Of course, this only works as long as [[UIScreen mainScreen] scale] operates as it currently does.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top