CGContextAddLineToPoint giving unexpected results

Discussion in 'iOS Programming' started by Denvildaste, Apr 21, 2010.

  1. Denvildaste macrumors newbie

    Joined:
    Apr 7, 2010
    #1
    Hello, I'm trying to draw a single line, the code works but the line is not the right color and is 2 pixels thick despite me explicitly telling it to use 1 pixel as line width, what's more confusing is that if I increase the line width the color changes! what am I doing wrong? here's my code:

    Code:
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextClearRect(ctx, rect);
    CGContextSetLineWidth(ctx, 1) ;
    CGContextBeginPath(ctx);
    CGContextSetRGBStrokeColor(ctx, (CGFloat)255/255, (CGFloat)0/255, (CGFloat)0/255, 1.0);
    CGContextMoveToPoint(ctx, 0, 450);
    CGContextAddLineToPoint(ctx, 320, 450);
    CGContextStrokePath(ctx);
    
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
  3. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #3
    Canonical form for a 8-bit color component constant, but converted to match the API.

    I prefer (CGFloat)0.0/255.0 ;
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    I would just write (CGFloat) 0.0 personally...

    Edit to add: try drawing between the pixels (seriously). I know on Mac OSX if you want a perfect one pixel line drawing at x.5 gets that. Note sure if the iPhone is the same...
     
  5. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #5
    Or just use CGContextSetShouldAntialias() passing false.
     

Share This Page