18

When I draw lines and shapes etc I get the " current graphics context" in iOS.

What exactly though is " current graphics context" - I'm looking for the 30,000 foot description.

Right now I just copy and paste UI code, not exactly sure what it's doing.

mfaani
  • 33,269
  • 19
  • 164
  • 293
Ian Vink
  • 66,960
  • 104
  • 341
  • 555
  • 1
    I'm not really sure what a "30,000 foot description" is. Do you mean a very comprehensive and detailed explanation? Here's [Apple's documentation on the Graphics Context](http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_overview/dq_overview.html#//apple_ref/doc/uid/TP30001066-CH202-TPXREF131). – Cody Gray - on strike Jan 23 '11 at 05:11
  • 5
    A "30,000 foot view" is also known as "the big picture". At 30,000 feet above the earth many details on the ground are obscure to the naked eye. See also the 4th entry in the top Google result for "30000 foot view phrase": http://askthemanager.com/2008/11/the-25-most-annoying-business-phrases/ – Old McStopher Aug 09 '11 at 07:35
  • For a clear explanation of the the broad view, try this ... http://stackoverflow.com/questions/4775594/what-is-a-graphic-context-ios/4776606#4776606 – Fattie Mar 06 '12 at 22:06

3 Answers3

17

A graphics context is the place where information about the drawing state is stored. This includes fill color, stroke color, line width, line pattern, winding rule, mask, current path, transparency layers, transform, text transform, etc. When using CoreGraphics calls, you specify the context to use to every single function. This means you can use multiple contexts at once, though typically you only use one. At the UIKit layer, there is the concept of a "current" graphics context, which is a graphics context that's used by all UIKit-level drawing calls (such as -[UIColor set] or UIBezierPath drawing). The current context is stored in a stack of contexts, so you can create a new context for some drawing, then when you finish with it the previous context is restored. Typically you get a context for free inside of -[UIView drawRect:] inside of CALayer display-related methods, but not otherwise.

It used to be that the "current" context was an application-wide global state, and therefore was not safe to touch outside of the main thread. As of iOS 4.0 (I believe), this became a thread-local state and UIKit-level drawing methods became safe to use on background threads.

mfaani
  • 33,269
  • 19
  • 164
  • 293
Lily Ballard
  • 182,031
  • 33
  • 381
  • 347
  • What do you mean we get a context for free when inside drawRect? – mskw Dec 06 '12 at 22:40
  • @mskw: It means that inside of `-drawRect:`, the frameworks have already set up a graphics context and set it as the "current context", so your UIKit drawing commands will work without worrying about contexts. Outside of `-drawRect:` that's typically not true. – Lily Ballard Dec 06 '12 at 22:57
  • But you still need to call the UIGraphicsBeginImageContextWithOptions function right? – mskw Dec 06 '12 at 23:26
  • 1
    @mskw: In `-drawRect:`? Absolutely not. Doing so would mean you're drawing into a brand new graphics context, and not the one that the `UIView` (or `CALayer`) is going to actually show on-screen. – Lily Ballard Dec 07 '12 at 00:21
  • @mskw: You can think of it as if the framework code looks like this pseudocode: `UIGraphicsBeginImageContextWithOptions(view.bounds, ...); [yourView drawRect:view.bounds]; UIImage *image = UIGraphicsGetImageFromContext(); UIGraphicsEndImageContext(); saveImageToLayerAndDrawToScreen(image);` – Lily Ballard Dec 07 '12 at 00:22
16

The OS needs a place to save information, such as drawing state, which you don't want to specify in every single CG drawing command, such as in which bitmap or view to draw, the scale or other transform to use, the last color you specified, etc.

The context tells each CG call where to find all this "stuff" for your current drawing call. Give a different context to the exact same drawing call, and that call might draw to a different bitmap in a completely different view, with a different color, different scale, etc.

hotpaw2
  • 70,107
  • 14
  • 90
  • 153
  • _Give a different context to the exact same drawing call_ can you show what you mean with some code? – mfaani Nov 12 '20 at 22:53
1

Basically it is a class in a platform (iOS, Android, JavaME and many others) that provides access to all the drawing/display capabilities provided for that platform. It varies a bit for different platforms of course, but this is the 30,000 foot description :)

omermuhammed
  • 7,365
  • 4
  • 27
  • 40
  • FYI, on iOS, functions relating to getting the current graphics context are merely UIKit reference functions, not "classes". ;) – Old McStopher Aug 09 '11 at 07:42
  • I'd also refer to a "context" as a "state" more so than a "class". A class may be one way of implementing it, though. – Old McStopher Aug 09 '11 at 07:53