【问题标题】:CGContextSetLineWidth(context, 1) - the width is almost alwayas at least 2 pixels instead 1CGContextSetLineWidth(context, 1) - 宽度几乎总是至少 2 个像素而不是 1
【发布时间】:2010-01-09 12:23:34
【问题描述】:

CGContextSetLineWidth(context, 1) 的宽度几乎总是至少 2 像素而不是 1

QQCandleStickLayer.m

-(id)init
{
    self = [super init];
    if(self != nil)
    {       
        self.delegate = self;       
        self.opaque = NO;       
    }
    return self;
}

- (void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context{

    CGContextSetLineWidth(context, 1.0f);
    CGContextSetAllowsAntialiasing(context, false);
    CGContextSetShouldAntialias(context, false);
    CGContextSetInterpolationQuality(context,kCGInterpolationNone);
    CGContextSetShadowWithColor(context, CGSizeMake( 0.0, 0.0 ), 0, NULL);


    CGContextMoveToPoint(context, self.bounds.origin.x+30.5f, self.bounds.origin.y+self.bounds.size.height-indent);
    CGContextAddLineToPoint(context, self.bounds.origin.x+30.5f, self.bounds.origin.y+self.bounds.size.height-(self.bounds.size.height-lastY)); 
    CGContextClosePath(context);
    CGContextSetRGBStrokeColor(context, 119.0f/255, 119.0f/255, 119.0f/255, 1.0);
    CGContextStrokePath(context);
}

QQDefluviumLayer.m

- (void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context{  
    UIImage *myDefluvium = [UIImage imageNamed:@"delfluviumNewRotatedSmall.png"];
    CGLayerRef layerCircle = CGLayerCreateWithContext(context, myDefluvium.size,NULL);
    if (layerCircle)
    {
        CGContextRef layerContext = CGLayerGetContext(layerCircle);
        CGContextDrawImage(layerContext, (CGRect){ CGPointZero, myDefluvium.size }, myDefluvium.CGImage);       
        CGContextDrawLayerInRect(context, CGRectMake(layer.bounds.origin.x,layer.bounds.origin.y, layer.bounds.size.width,layer.bounds.size.height), layerCircle);
        CFRelease(layerCircle);
    }
}

QuickQuoteViewController.m

defluviumLayer=[[QQDefluviumLayer alloc] init];
    [defluviumLayer setBounds:CGRectMake(0, 0, 61, 343)];
    [defluviumLayer setPosition:CGPointMake(277,246)];
    [self.view.layer addSublayer:defluviumLayer];
    [defluviumLayer update];


    candleStickLayer=[[QQCandleStickLayer alloc] init];
    [candleStickLayer setBounds:CGRectMake(0,0, defluviumLayer.frame.size.width, defluviumLayer.frame.size.height)];
    [candleStickLayer setPosition:CGPointMake(defluviumLayer.position.x,defluviumLayer.position.y)];
    [self.view.layer addSublayer:candleStickLayer];
    [candleStickLayer update];

我在 CALayer 中绘制,并且在层下有图像,正如我测试过的那样 - 如果我在清晰的白色视图上绘制 - 可以以宽度 1 绘制线条,但不能在图像上绘制

【问题讨论】:

    标签: iphone quartz-graphics


    【解决方案1】:

    线宽确实是 1 像素宽,但一侧宽 0.5 像素,另一侧宽 0.5 像素。将其粘贴到像素网格上,使其两侧看起来都是 1 像素宽、50% 不透明度,使其成为 2 像素宽、50% 不透明度的线。

    为避免这种情况,请在半像素坐标上画线。

    例如,代替

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

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

    【讨论】:

    • CGContextSetLineWidth(context, 1.0f); CGContextSetAllowsAntialiasing(上下文,假); CGContextSetShouldAntialias(上下文,假); CGContextSetShadowWithColor(context, CGSizeMake( 0.0, 0.0 ), 0, NULL); CGContextSetRGBStrokeColor(上下文, 119.0f/255, 119.0f/255, 119.0f/255, 1.0); CGContextMoveToPoint(context, self.bounds.origin.x+30.5f, self.bounds.origin.y+self.bounds.size.height-indent); CGContextAddLineToPoint(context, self.bounds.origin.x+30.5f, self.bounds.origin.y+self.bounds.size.height-(self.bounds.size.height-lastY)); CGContextClosePath(context);
    • 您能否发布代码(通过编辑您的问题)以包含 CALayer 和图像部分?根据您的描述,关键似乎是“但不在图像上”。
    【解决方案2】:

    像素点像KennyTM所说的那样位于2个像素之间,并且该行在显示前默认为Antialiased,因此显示为width = 2.0,关闭Antialias也可以解决问题。

    CGContextSetShouldAntialias(context, NO);
    

    来自:sfkaos on iphonedevsdk

    我试过了,效果很好。

    【讨论】:

    • 这对我有帮助。谢谢。我以为是这样的,因为它看起来很模糊:-)
    【解决方案3】:

    我同意 KennyTM,但是我必须将 0.5 添加到 Y 轴,而不是 X 轴,以使我的线显示一半。我想这取决于你的线的走向,即水平或垂直!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-23
      • 2020-08-27
      相关资源
      最近更新 更多