0

I found this in the Quartz 2D Programming Guide:

To draw to the screen in an iOS application, you set up a UIView object and implement its drawRect: method to perform drawing. The view’s drawRect: method is called when the view is visible onscreen and its contents need updating. Before calling your custom drawRect: method, the view object automatically configures its drawing environment so that your code can start drawing immediately. As part of this configuration, the UIView object creates a graphics context (a CGContextRef opaque type) for the current drawing environment. You obtain this graphics context in your drawRect: method by calling the UIKit function UIGraphicsGetCurrentContext.

Since I am having problems with invalid Context (because it's 0x00 when I go back to re-draw), I was wondering if I could get the current context in the beginning of -drawRect and somehow pass it to the methods I call from within -drawRect?

SpokaneDude
  • 4,856
  • 13
  • 64
  • 120
  • 1
    I answered below as well but just wanted to check: you're not calling `drawRect` directly, right? It's called by UIKit only when needed. If you want to update your view, you should be calling `setNeedsDisplay` instead. – Cameron Spickert Apr 09 '13 at 17:41

4 Answers4

3

You can definitely pass CGContextRef to methods called from drawRect: as long as these methods do not save the reference for use outside the duration of the drawRect: call, your code should be fine. However, the context reference that you pass around would be equivalent to the context retrieved through UIGraphicsGetCurrentContext, so I doubt that there is much to gain by adding an extra parameter.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Hey guys... that's not what's going on... the docs state: **You obtain this graphics context in your drawRect: method by calling the UIKit function UIGraphicsGetCurrentContext.** I was calling UIGraphicsGetCurrentContext from the called methods and it was returning 0x00 (see my question above)... I've got three different answers here; I guess the best way is to do it and see... I'll get back to all of you. – SpokaneDude Apr 09 '13 at 16:55
  • @spokane-dude You may want to share some code where you call something from `drawRect:` and get a `NULL` for your `UIGraphicsGetCurrentContext` then: a lot of different problems may be in place, including examining/printing the context ref incorrectly. – Sergey Kalinichenko Apr 09 '13 at 16:58
  • For starters, you can **not** call methods from within -drawRect and pass a context that is considered valid. However, if I get the current context in drawRect before calling the other methods, it seems to work... needs more testing, but I think I found the problem: call UIGraphicsGetCurrentContext at the beginning of drawRect! Thanks everybody! – SpokaneDude Apr 09 '13 at 17:14
  • What a delimma because nether answer is totally correct; apparently you **must** call UIGraphicsGetCurrentContext from within -drawRect in order for the called methods to work, but you can't pass the context -- it's flagged as invalid at run-time. What would you guys like me to do? – SpokaneDude Apr 09 '13 at 17:24
  • @spokane-dude I strongly doubt the generality of your finding: I have no doubt that what you describe is true in your specific case, but I'm also sure that my code obtains graphic context in the middle of `drawRect`, and also in other methods called from `drawRect` without a smallest trace of a problem. Moreover, there is [another question](http://stackoverflow.com/q/6104736/335858) that discuss the relative performance of obtaining a graphic context vs. passing it around, which implies that both methods work fine. If you posted some code, we could look for what could be wrong with it. – Sergey Kalinichenko Apr 09 '13 at 17:33
  • dasblinkenlight: too much code to post, in addition I would need a NDA... it's working for me -- I get the graphics context in -drawRect and the other methods that are called, without passing it in the call. I'll give you the points since you seem to have it working both ways. Anyway, thank you so much for your help... at least it helped me get it to work. :D – SpokaneDude Apr 09 '13 at 18:27
  • @dasblinkenlight I'm almost positive (from my empirical testing in iOS) that passing `CGContextRef` produces a leak. I have no idea why this would be, but I'm pretty sure it's true, even if the methods called only move and draw paths. This seems to be the case only for contexts derived from `UIGraphicsGetCurrentContext`... – Dan Rosenstark May 30 '14 at 22:28
2

UIGraphicsGetCurrentContext can only be called from drawRect: method (or methods called from it) otherwise it will return nil.

msk
  • 8,885
  • 6
  • 41
  • 72
2

You can use UIGraphicsGetCurrentContext from any method called from -drawRect. It's worth noting that you should not call -drawRect directly when you need to update your view; call -setNeedsDisplay instead.

If you want to use the UIKit drawing system with your own off-screen context, you can use UIGraphicsPushContext to set the current context.

Cameron Spickert
  • 5,190
  • 1
  • 27
  • 34
0

In my experience, passing CGContextRef produces a memory leak that's pretty "fast."

Dan Rosenstark
  • 68,471
  • 58
  • 283
  • 421