2010-01-09 15 views
13

Con CGContextSetLineWidth(context, 1) la anchura es casi alwayas al menos 2 píxeles en lugar 1CGContextSetLineWidth (contexto, 1) - el ancho es de casi alwayas al menos 2 píxeles en lugar 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]; 

estoy dibujando en una CALayer, y tienen Image con la imagen debajo de la capa, como he probado - Si dibujo en clara vista blanco - la línea se puede extraer con un ancho de 1, pero no en la imagen

Respuesta

28

El ancho de línea es de hecho de 1 píxel de ancho, pero de 0,5 píxeles de ancho en un lado y de 0,5 píxeles en el otro lado. Pegarlo a las cuadrículas de píxeles hace que parezca tener 1 píxel de ancho con un 50% de opacidad en ambos lados, lo que lo convierte en una línea de 2 píxeles de ancho con un 50% de opacidad.

Para evitar esto, dibuje la línea en coordenadas de medio píxel.

Por ejemplo, en lugar de

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

hacer

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

Una explicación relacionada: https://developer.mozilla.org/En/Canvas_tutorial/Applying_styles_and_colors#A_lineWidth_example – kennytm

+0

CGContextSetLineWidth (context, 1.0f); \t CGContextSetAllowsAntialiasing (context, false); \t CGContextSetShouldAntialias (context, false); \t CGContextSetShadowWithColor (context, CGSizeMake (0.0, 0.0), 0, NULL); \t \t CGContextSetRGBStrokeColor (contexto, 119.0f/255, 119.0f/255, 119.0f/255, 1.0); \t CGContextMoveToPoint (context, self.bounds.origin.x + 30.5f, self.bounds.origin.y + self.bounds.size.height-indent); \t CGContextAddLineToPoint (context, self.bounds.origin.x + 30.5f, self.bounds.origin.y + self.bounds.size.height- (self.bounds.size.height-lastY)); \t CGContextClosePath (context); – freennnn

+0

muchas gracias, pero desafortunadamente no me ayudó ( – freennnn

22

el punto de pixel se encuentra entre 2 píxeles como KennyTM dijo, y la línea por defecto se suavizadas antes de ir de visualización, por lo que aparece como ancho = 2.0, apague Antialias puede solucionar el problema.

CGContextSetShouldAntialias(context, NO); 

de: sfkaos on iphonedevsdk

he intentado, me da buenos resultados.

+0

Esto me ayudó. Gracias. Pensé que era algo así ya que parecía borroso :-) – Christopher

0

Estoy de acuerdo con KennyTM, sin embargo tuve que agregar el 0.5 al eje Y, no el eje X para que mi línea muestre la mitad. ¡Supongo que depende de qué dirección vaya tu línea, es decir, horizontal o vertical!

Cuestiones relacionadas