3

From Apple iOS Documentation #UIGraphicsGetCurrentContext: "In iOS 4 and later, you may call this function from any thread of your app."

Also, why is the method call in C function format UIGraphicsGetCurrentContext() and not Objective-C message format [UIView UIGraphicsGetCurrentContext]?

user
  • 3,388
  • 7
  • 33
  • 67

1 Answers1

2

There are some APIs on iOS that are accessed via C and not ObjC objects. CoreGraphics for drawing (all of the CG* functions) is one of the most important ones.

Direct drawing is typically done into a CGContextRef, which represents a graphics context. UIKit keeps a stack of contexts for drawing (typically you won't interact much beyond the current context). This stack is accessed by global C functions because you can use them from anywhere. Typically, they are accessed from inside of drawRect:, but you can use this stack elsewhere. Per the docs:

If you are not using a UIView object to do your drawing, however, you must push a valid context onto the stack manually using the UIGraphicsPushContext function.

Ben Zotto
  • 70,108
  • 23
  • 141
  • 204
  • Can you complete the following sentence: "You need a graphic context when..." Is it when I need to draw a line, move from point to point? stroke and fill it? What else? – mfaani Jun 28 '19 at 03:08