UIGraphicsPushContext, UIGraphicsPopContext

Hi fellow Nerds… I’m pulling my hair out. I want to make sure I understand the subject calls. Below is may example that is NOT working as hoped.

Summary: I want to draw three lines. Two lines are of width 5. and drawn from drawRect. A single line of width 25. is drawn from the drawBigLine method.

My code returns three lines. The last two lines drawn are of width 25. I was expecting the UIGraphicsPopContext to return me back to the context with lineWidth value of 5. That is not happening. It seems that UIGraphicsPopContext is not popping.

Any ideas of what I’m doing wrong?

Much Thanks – Ron

@implementation RLView

  • (void)drawRect:(CGRect)rect
    {
    CGContextRef ctx=UIGraphicsGetCurrentContext();
    CGContextBeginPath(ctx);
    CGContextMoveToPoint(ctx, 100., 100.);
    CGContextAddLineToPoint(ctx, 200., 200.);
    CGContextSetLineWidth(ctx, 5.);
    CGContextDrawPath(ctx, kCGPathStroke);

    [self drawBigLine:ctx];

    CGContextBeginPath(ctx);
    CGContextMoveToPoint(ctx, 300., 10.);
    CGContextAddLineToPoint(ctx, 300., 300.);
    CGContextDrawPath(ctx, kCGPathStroke);
    }

-(void)drawBigLine:(CGContextRef)context{

UIGraphicsPushContext(context);

CGContextBeginPath(context);
CGContextMoveToPoint(context, 30., 30.);
CGContextAddLineToPoint(context, 90., 90.);
CGContextSetLineWidth(context, 25.);
CGContextDrawPath(context, kCGPathStroke);

UIGraphicsPopContext();

}

@end

If I add the following:

[b]CGContextSaveGState(ctx);[/b]
[self drawBigLine:ctx];
[b]CGContextRestoreGState(ctx);[/b]

all works as expected.