gpt4 book ai didi

iphone - 为什么我的线条越来越粗?

转载 作者:行者123 更新时间:2023-12-03 20:58:54 25 4
gpt4 key购买 nike

我尝试用不同的颜色画一些线条。

此代码尝试用 1px 细线绘制两个矩形。但是,第二个矩形是用 2px 宽度的线绘制的,而第一个矩形是用 1px 宽度绘制的。

- (void)addLineFrom:(CGPoint)p1 to:(CGPoint)p2 context:(CGContextRef)context {  
// set the current point
CGContextMoveToPoint(context, p1.x, p1.y);

// add a line from the current point to the wanted point
CGContextAddLineToPoint(context, p2.x, p2.y);
}


- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();

CGPoint from, to;



// ----- draw outer black frame (left, bottom, right) -----
CGContextBeginPath(context);

// set the color
CGFloat lineColor[4] = {26.0f * colorFactor, 26.0f * colorFactor, 26.0f * colorFactor, 1.0f};
CGContextSetStrokeColor(context, lineColor);

// left
from = CGPointZero;
to = CGPointMake(0.0f, rect.size.height);
[self addLineFrom:from to:to context:context];

// bottom
from = to;
to = CGPointMake(rect.size.width, rect.size.height);
[self addLineFrom:from to:to context:context];

// right
from = to;
to = CGPointMake(rect.size.width, 0.0f);
[self addLineFrom:from to:to context:context];

CGContextStrokePath(context);
CGContextClosePath(context);



// ----- draw the middle light gray frame (left, bottom, right) -----

CGContextSetLineWidth(context, 1.0f);
CGContextBeginPath(context);

// set the color
CGFloat lineColor2[4] = {94.0f * colorFactor, 94.0f * colorFactor, 95.0f * colorFactor, 1.0f};
CGContextSetStrokeColor(context, lineColor2);

// left
from = CGPointMake(200.0f, 1.0f);
to = CGPointMake(200.0f, rect.size.height - 2.0f);
[self addLineFrom:from to:to context:context];

// bottom
from = to;
to = CGPointMake(rect.size.width - 2.0f, rect.size.height - 2.0f);
[self addLineFrom:from to:to context:context];

// right
from = to;
to = CGPointMake(rect.size.width - 2.0f, 1.0f);
[self addLineFrom:from to:to context:context];

// top
from = to;
to = CGPointMake(1.0f, 1.0f);
[self addLineFrom:from to:to context:context];

CGContextStrokePath(context);
}

最佳答案

如果没记错的话,默认情况下会启用抗锯齿功能,这可能会导致绘图影响的像素数量超出您的预期。关闭 CGContextRef 的抗锯齿功能,看看是否有帮助。

iOS:

CGContextRef context = UIGraphicsGetCurrentContext();
[context setShouldAntialias:NO];

苹果机:

CGContextRef context = [NSGraphicsContext currentContext];
[context setShouldAntialias:NO];

关于iphone - 为什么我的线条越来越粗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2684769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com