开发者

Context made using UIGraphicsPushContext does not show rendered images

开发者 https://www.devze.com 2023-01-22 13:16 出处:网络
I am using UIGraphicsPushContext to push a context inside drawRect like so: - (void)drawRect:(CGRect)rect {

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);
  // Dr开发者_如何学编程aw 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!


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];
}


I think you miss this :

- (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

  //blah blah

  UIGraphicsPopContext();
}

using UIGraphicsPopContext() to restore previous context

0

精彩评论

暂无评论...
验证码 换一张
取 消