stinkymatt
stinkymatt

Reputation: 1678

Changing colors with CGContextStrokePath

I'm trying to draw some simples lines with the iPhone/Touch SDK. I'd like to be able to change the color of the lines, but calling CGContextSetRGBStrokeColor doesn't seem to affect the drawn lines that are made with CGContextAddLineToPoint until the actual call to CGContextStrokePath is made. So if I make multiple calls to change the color, only the one made just before CGContextStrokePath has any effect. Here's what I mean:

    - (void)drawRect:(CGRect)rect 
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextMoveToPoint(ctx, 0, 0);
    CGContextSetRGBStrokeColor(ctx,1,0,0,1);
    CGContextAddLineToPoint(ctx, 100, 100);
    //CGContextStrokePath(ctx);
    CGContextSetRGBStrokeColor(ctx,0,1,0,1);
    CGContextAddLineToPoint(ctx, 200, 300);
    //CGContextStrokePath(ctx);
    CGContextSetRGBStrokeColor(ctx,0,0,1,1);
    CGContextStrokePath(ctx);
}

I assume I'm doing something horribly wrong, I just can't quite figure out what. I thought that if I added the CGContextStrokePath calls, that would help, it doesn't.

See discussion below for how I got to the corrected code:

- (void)drawRect:(CGRect)rect 
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextBeginPath(ctx);
    CGContextMoveToPoint(ctx, 0, 0);
    CGContextSetRGBStrokeColor(ctx,1,0,0,1);
    CGContextAddLineToPoint(ctx, 100, 100);
    CGContextStrokePath(ctx);
    CGContextClosePath(ctx);
    CGContextBeginPath(ctx);
    CGContextMoveToPoint(ctx, 100, 100);
    CGContextSetRGBStrokeColor(ctx,0,1,0,1);
    CGContextAddLineToPoint(ctx, 200, 300);
    CGContextStrokePath(ctx);
}

Upvotes: 5

Views: 7152

Answers (1)

Pierce Hickey
Pierce Hickey

Reputation: 1426

I don't think you're doing anything horribly wrong, just that CGContextStrokePath for a given Graphics Context can only have one RGBStrokeColor at a time. As a result, multiple calls to CGContextStrokePath are required, once for each colour.

A reference (not a copy) to the Graphics Context is added to that stack of drawing operations with each call to CGContextAddLineToPoint() . When you finally call CGContextStrokePath(), the last value for RGBStrokeColor is used.

If you want to use multiple colours with the same Graphics Context, then it appears that you need to make multiple calls to CGContextStrokePath() changing the value of RGBStrokeColor() on the Graphics Context between calls. The Apple sample code in AccelerometerGraph/GraphView.m appears to indicate this also.

Upvotes: 4

Related Questions