2010-04-21 9 views
2

J'essaie de dessiner des lignes avec des couleurs différentes.Pourquoi mes lignes deviennent-elles plus épaisses et épaisses?

Ce code tente de dessiner deux rectangles avec des lignes fines 1px. Cependant, le deuxième rectangle est dessiné avec des lignes de largeur 2px, tandis que le premier est dessiné avec une largeur de 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); 
} 
+0

Épaississement de ligne: Cela nous concerne tous! :-) – Smandoli

+0

shoud je devine quelque chose? Ou dites-vous que ces problèmes d'épaisseur de ligne sont très communs? – dontWatchMyProfile

Répondre

2

Si ma mémoire est bonne, anticrénelage est activé par défaut qui peut causer plus de pixels d'être affectés par votre dessin que vous avez l'intention. Désactivez l'antialiasing pour votre CGContextRef et voyez si cela aide.

iOS:

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

Mac:

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

comment désactiver l'antialiasing de CGContextRef ?? – harshit2811

+5

'CGContextSetAllowsAntialiasing (context, false);' désactive l'antialiasing d'un 'CGContextRef'. – Raginmari

1

Le premier semble être un pixel épais parce que vous avez dessiné autour du périmètre de la rect sale, qui UIView clipsée pour vous, en faisant un coup intérieur. Mais, en fait, les deux rectangles ont le même problème.

Sur une autre question, j'ai écrit a full description of the real problem and the real solutions. Désactiver AA suffira pour les lignes droites, mais vous détesterez dès que vous dessinez pivoté ou dessinez une diagonale.