title: <Error>: CGContextSaveGState: invalid context 0x0 Error
body: <p>I am getting some issue related with preparing the <code>UIImage</code> out of a <code>UIView</code>. I sometimes (but not always) see the error</p>
  
  <pre class="lang-none prettyprint-override"><code>&lt;Error&gt;: CGContextSaveGState: invalid context 0x0
  </code></pre>
  
  <p>I'm using the iPhone SDK 4.0. My code:</p>
  
  <pre><code>-(void)drawRect:(CGRect)rect
  {
       // Customized to draw some text
  }
  
  
  -(UIImage*) PrepareBackImage:(CGRect) aRect // aRect = (0,0,1800,1200)
  {
      UIImage* background;
  
      background      = [self GetImageFromView:self toRect:self.frame];
      UIGraphicsBeginImageContext(aRect.size);
  
      CGPoint backgroundPoint = CGPointMake(0,0);
      [background drawAtPoint:backgroundPoint];
  
      UIImage* backImage = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
  
      return backImage;
  }
  
  - (UIImage *) GetImageFromView:(UIView *)aView toRect:(CGRect)aRect
  {
      CGSize pageSize = aRect.size;
      UIGraphicsBeginImageContext(pageSize);
      [aView.layer renderInContext:UIGraphicsGetCurrentContext()];
      UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
      return image;
  }
  </code></pre>