iOS " current graphics context" - What is that
Asked Answered
A

3

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.

Araby answered 23/1, 2011 at 4:59 Comment(3)
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.Diatessaron
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": askthemanager.com/2008/11/the-25-most-annoying-business-phrasesMulish
For a clear explanation of the the broad view, try this ... #4776094Tonisha
B
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.

Blackout answered 23/1, 2011 at 5:54 Comment(1)
Give a different context to the exact same drawing call can you show what you mean with some code?Gripping
A
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.

Arapaho answered 23/1, 2011 at 6:41 Comment(5)
What do you mean we get a context for free when inside drawRect?Signor
@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.Arapaho
But you still need to call the UIGraphicsBeginImageContextWithOptions function right?Signor
@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.Arapaho
@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);Arapaho
B
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.

Blackout answered 23/1, 2011 at 5:54 Comment(1)
Give a different context to the exact same drawing call can you show what you mean with some code?Gripping
S
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 :)

Syck answered 23/1, 2011 at 5:11 Comment(2)
FYI, on iOS, functions relating to getting the current graphics context are merely UIKit reference functions, not "classes". ;)Mulish
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.Mulish

© 2022 - 2024 — McMap. All rights reserved.