views:

37

answers:

1

I am using UIGraphicsPushContext to push a context inside drawRect like so:

- (void)drawRect:(CGRect)rect {
  // Create Bitmap Graphics Context
  UIContextRef ctxt = //blah blah;
  // Push the custom context in the graphics stack
  UIGraphicsPushContext(ctxt);
  // Draw Stuff
  // Get Image from custom context
} 

My problem is that the image is not shown on screen(on the view) but I can confirm that I have successfully drawn on the context since the image retrieved from the context shows an image similar to what I intended to draw.

What I wanted to achieve is replace my call of UIGraphicsGetCurrentContext() (which works and shows the image) and use (push) a custom context that I can utilize.

Am I missing something here? Thanks!

A: 

If you want to draw to an image and then also show it in the view you have to either draw it on the view and then also to an image, or first draw to an image an then draw that image on the view. To implement the second option do something like this:

- (void)drawRect:(CGRect)rect {
    // Create image
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    // Draw the image content using ctx

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    // Draw image on view
    [image drawAtPoint:CGPointZero];
}
loomer
hmm, I know I can do this and this doesn't answer my question. And, drawAtPoint and CGContextDrawImage tends to be slow on high pixel density screens. Thanks anyway :)
LaN
ok, then the only solution I see is to draw first on the view and then to a image context. But it might also be the case that I still don't understand your question :)
loomer