title: Multiple CGContextRef in drawRect
body: <p>In overriding a UIView drawRect, I draw the main image with <code>CGContextDrawImage</code>. Over it I need to draw another image (with a multiply blend mode), so I actually need to draw <em>over</em> it.</p>
  
  <p>This second image needs to be prepares since it's generated dynamically (it has to be masked, may be different size and so), so in the end I need to generate it. How can I get a second context where I can draw and mask this second image <strong>before</strong> applying it over the main one with? If I draw on the current context, it gets directly drawn no the main one before I can mask it.</p>
  
  <pre><code>- (void) drawRect:(CGRect)rect
  {
      CGContextRef ctx = UIGraphicsGetCurrentContext();
  
      CGContextDrawImage(ctx, CGRectMake(0, 0, rect.size.width, rect.size.height), [UIImage imageNamed:@"actionBg.png"].CGImage);
  
      // prevent the drawings to be flipped
      CGContextTranslateCTM(ctx, 0, rect.size.height);
      CGContextScaleCTM(ctx, 1.0, -1.0);
  
      // generate the overlay
      if ([self isActive] == NO &amp;&amp; self.fullDelay != 0) { // TODO: remove fullDelay check!
          int segmentSize = (ACTION_SIZE / [self fullDelay]);
  
          for (int i=0; i&lt;[self fullDelay]; i++) {
              float alpha = 0.9 - (([self fullDelay] * 0.1) - (i * 0.1));
              [[UIColor colorWithRed:120.0/255.0 green:14.0/255.0 blue:14.0/255.0 alpha:alpha] setFill];
  
              if (currentDelay &gt; i) {
                  CGRect r = CGRectMake(0, i * segmentSize, ACTION_SIZE, segmentSize);
                  CGContextFillRect(ctx, r);
              }
              [[UIColor colorWithRed:1 green:1 blue:1 alpha:0.3] setFill];
              CGRect line = CGRectMake(0, (i * segmentSize) + segmentSize - 1 , ACTION_SIZE, 1);
              CGContextFillRect(ctx, line);
          }
  
          UIImage *overlay = UIGraphicsGetImageFromCurrentImageContext();
          UIImage *overlayMasked = [TDUtilities maskImage:overlay withMask:[UIImage imageNamed:@"actionMask.png"]];
      }
  }
  </code></pre>
  
  <p>Here overlayMasked now contains the correct image, but since I've prepared it using the main context, its is not all messed. Thanks</p>
  
  <p>Thanks</p>