CGContextSetLineWidth(context, 1) - the width is almost alwayas at least 2 pixels instead 1

拈花ヽ惹草 提交于 2019-12-04 00:09:40

The line width is indeed 1 pixel wide, but 0.5 pixel wide on one side and 0.5 pixel wide on the other side. Sticking it to the pixel grids makes appears to be 1 pixel wide at 50% opacity on both sides, making it a 2 pixel wide line at 50% opacity.

To avoid this, draw the line on half-pixel coordinates.

For example, instead of

CGContextMoveToPoint(ctx, 20.0f, 30.0f);
CGContextAddLineToPoint(ctx, 20.0f, 100.0f);

do

CGContextMoveToPoint(ctx, 20.5f, 30.0f);
CGContextAddLineToPoint(ctx, 20.5f, 100.0f);

the point of pixel is located between 2 pixels like KennyTM said, and the line by default is Antialiased before go display, so it appears as width = 2.0, turn off Antialias can fix the problem either.

CGContextSetShouldAntialias(context, NO);

from: sfkaos on iphonedevsdk

I tried, works fine for me.

I agree with KennyTM, however I had to add the 0.5 to the Y axis, not the X axis to get my line to show half. I guess it depends which way your line goes, i.e. horizontal or vertical!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!