首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CGContext线宽

CGContext线宽
EN

Stack Overflow用户
提问于 2013-08-27 08:11:22
回答 2查看 9.6K关注 0票数 7

我试着画一条简单的线,问题是它的宽度不是1个像素,而是2个像素。文档指出,如果我没有读错,用户空间单位将转换为一个像素。

代码非常简单,但是我的代码行总是2个像素宽。

代码语言:javascript
复制
//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);

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-08-27 08:16:38

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

票数 3
EN

Stack Overflow用户

发布于 2013-08-27 10:25:54

我在一个测试应用程序中尝试了以下内容:

代码语言:javascript
复制
CGFloat desiredWidthInPixels = 1.0f;
CGFloat lineWidth = desiredWidthInPixels / [[UIScreen mainScreen] scale];
NSLog(@"lineWidth = %f", lineWidth);

我得到的lineWidth是0.5f,应该可以准确地转换为屏幕上的单像素宽度(因为scale是2.0,表示“视网膜”设备)。这应该适用于不同屏幕尺寸的设备。

*警告:当然,这只适用于[[UIScreen mainScreen] scale]当前的运行方式。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18455024

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档