繁体   English   中英

CGContext线宽

[英]CGContext line width

我试图绘制一条简单的线,问题是它的宽度不是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);

在此输入图像描述

没有点不会转换为像素。 如果您的线太粗,请更改该值。

我在测试应用中尝试了以下内容:

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

我得到了一个0.5f的lineWidth,它应该准确地转换为屏幕上的单个像素宽度(因为比例是2.0,表示“Retina”设备)。 这应该适应不同屏幕尺寸的设备。

***警告:当然,只有[[UIScreen mainScreen] scale]像现在一样运行时, [[UIScreen mainScreen] scale]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM