문제

Here is my code:

CGContextSetShouldAntialias(context, NO);
CGContextSetLineWidth(context, 0.5);

CGPoint hPoints[2];
hPoints[0] = CGPointMake(10, 21.5);
hPoints[1] = CGPointMake(100, 21.5);

CGContextAddLines(context, hPoints, 2);

CGPoint vPoints[2];
vPoints[0] = CGPointMake(10, 21.5);
vPoints[1] = CGPointMake(10, 41.5);

CGContextAddLines(context, vPoints, 2);
CGContextStrokePath(context);

And here is the result:

enter image description here

as you can see vertical line is exact 1px width but horizontal line is stretched. I've tried all possible values for Y - (20, 20.5, 21, 21.5) every time line is stretched.

I have read a lot of threads here but with no success, can someone explain what is happening and how can I draw horizontal line on retina display of exact 1px width?

upd: Solved it by:

UIView *line = [[UIView alloc] initWithFrame:CGRectMake(10, 20, 100, 0.5)]; 
[line setBackgroundColor:[UIColor greenColor]]; 
[self addSubview:line]; 

1retina px without any stretches!

도움이 되었습니까?

해결책

Try this:

CGContextSetShouldAntialias(context, NO);
CGContextSetLineWidth(context, .5);

CGPoint hPoints[2];
hPoints[0] = CGPointMake(10, 22.0);
hPoints[1] = CGPointMake(100, 22.0);

CGContextAddLines(context, hPoints, 2);

CGPoint vPoints[2];
vPoints[0] = CGPointMake(10, 22.0);
vPoints[1] = CGPointMake(10, 42.0);

CGContextAddLines(context, vPoints, 2);
CGContextStrokePath(context);

If you draw a line and a point is a fraction then it will give a faint line. Hope this helps.. :)

EDIT:

Here is the link you are looking for I think. Read Points Versus Pixels. It describe the problem you are facing.

다른 팁

This is because you're drawing at 21.5 points.

When you use half a point it will try to place the line between two rows of pixels and to do this it tries to put half the line on each side (which obviously isn't possible) and so it draws a faint line on each side instead.

Just don't use half points and it will be fine.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top