Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS " current graphics context" - What is that

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.

like image 317
Ian Vink Avatar asked Jan 23 '11 04:01

Ian Vink


People also ask

What is a graphics context?

A graphics context defines basic drawing attributes such as the colors to use when drawing, the clipping area, line width and style information, font information, compositing options, and several others.

What is a context in core graphics?

A CoreGraphics context represents drawing state. It contains things such as the current transformation matrix, the width and height of any lines to be drawn, the fill color, the stroke color, and a bunch of other information about how the computer should draw things.

What is core graphics in IOS?

The Core Graphics framework is based on the Quartz advanced drawing engine. It provides low-level, lightweight 2D rendering with unmatched output fidelity.

What is UIGraphicsGetCurrentContext?

UIGraphicsGetCurrentContext()Returns the current graphics context.


2 Answers

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.

like image 118
Lily Ballard Avatar answered Oct 07 '22 07:10

Lily Ballard


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.

like image 26
hotpaw2 Avatar answered Oct 07 '22 06:10

hotpaw2