我试着画一条简单的线,问题是它的宽度不是1个像素,而是2个像素。文档指出,如果我没有读错,用户空间单位将转换为一个像素。
代码非常简单,但是我的代码行总是2个像素宽。
//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);

发布于 2013-08-27 08:16:38
没有点不会转换为像素。如果您的行太粗,请更改该值。
发布于 2013-08-27 10:25:54
我在一个测试应用程序中尝试了以下内容:
CGFloat desiredWidthInPixels = 1.0f;
CGFloat lineWidth = desiredWidthInPixels / [[UIScreen mainScreen] scale];
NSLog(@"lineWidth = %f", lineWidth);我得到的lineWidth是0.5f,应该可以准确地转换为屏幕上的单像素宽度(因为scale是2.0,表示“视网膜”设备)。这应该适用于不同屏幕尺寸的设备。
*警告:当然,这只适用于[[UIScreen mainScreen] scale]当前的运行方式。
https://stackoverflow.com/questions/18455024
复制相似问题