Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7407187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:40:24+00:00 2026-05-29T05:40:24+00:00

Very frustrating experience understanding UIGraphicsPushContext(context) and UIGraphicsPopContext() My understanding is that I can set

  • 0

Very frustrating experience understanding UIGraphicsPushContext(context) and UIGraphicsPopContext()

My understanding is that I can set attributes of the context like the stroke color and then push the context on a stack so I can set a new color for the current context. When I’m done doing that I can return to the context by popping it.

Below is my code. When I run the code below two lines are drawn in blue. What I expect to happen is: that I first set the color green. Go to the blueLine function and push the green context. Draw in blue. Then pop the green context. Allowing the drawLine function to drawn in green.

Here is a screenshot of what is drawn (two blue lines): http://dl.dropbox.com/u/1207310/iOS%20Simulator%20Screen%20shot%20Feb%205%2C%202012%209.00.35%20PM.png

Any help is GREATLY appreciated! Thank you.

- (void)drawBlueLine:(CGContextRef)context
{
    UIGraphicsPushContext(context);
    [[UIColor blueColor] setStroke];
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, self.bounds.origin.x, 100);
    CGContextAddLineToPoint(context, self.bounds.origin.x+self.bounds.size.width, 200);
    CGContextStrokePath(context); 
    UIGraphicsPopContext();
}

- (void)drawLine:(CGContextRef)context
{
    UIGraphicsPushContext(context);
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, self.bounds.origin.x, 200);
    CGContextAddLineToPoint(context, self.bounds.origin.x+self.bounds.size.width, 300);
    CGContextStrokePath(context); 
    UIGraphicsPopContext();
}

- (void)drawRect:(CGRect)rect
{   
    CGContextRef context = UIGraphicsGetCurrentContext();

    [[UIColor redColor] setStroke];
    CGContextSetLineWidth(context, 5.0);
    [self drawBlueLine:context];
    [self drawLine:context];
}

Or shouldn’t this work?

- (void)drawLine:(CGContextRef)oldContext
{

    UIGraphicsPushContext(oldContext); //makes oldContext the current context but there is a copy on the stack
    [[UIColor blueColor] setStroke];
    CGContextBeginPath(oldContext);
    CGContextMoveToPoint(oldContext, self.bounds.origin.x, 200);
    CGContextAddLineToPoint(oldContext, self.bounds.origin.x+self.bounds.size.width, 300);
    CGContextStrokePath(oldContext); 

    UIGraphicsPopContext(); //previous oldContext is moved back into place containing red?
}

- (void)drawRect:(CGRect)rect
{ 

    CGContextRef oldContext = UIGraphicsGetCurrentContext();

    [[UIColor redColor] setStroke];
    [self drawLine:oldContext];
    //anything I draw here should be red.  Shouldn't it?`
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:40:25+00:00Added an answer on May 29, 2026 at 5:40 am

    What UIGraphicsPushContext and UIGraphicsPopContext does as change which context is being used for drawing, not necessarily save and restore any given context’s state. What your code is doing is simply repetitively replacing the context that was current prior to the call to drawRect: with the context you grab with UIGraphicsGetCurrentContext (they are likely to be the same anyway).

    If you want to save an restore a given context’s state, use CGContextSaveGState and CGContextRestoreGState instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is very frustrating... I can set the Configuration File for a Windows Forms
Something is wrong and it is very frustrating. I read on velocity's homepage that
Been very frustrating but mysql is returning an empty set for this code (not
This question is sparked by a very frustrating problem that I find is a
I have no idea how to do that and it is very frustrating. I've
I've got a very frustrating SQL issue which i can't for the life of
In xcode 4.2 I have found it very frustrating because you can't use: -(void)dealloc
I'm working with COGNOS, a very frustrating BI application that relies heavily on Javascript.
I am experiencing a very frustrating issue when trying to insert a new record
I find it very frustrating doing web development with Maven & Jetty using Eclipse,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.