Does UIGraphicsBeginImageContext
use CGBitmapContextCreate
to create a graphics context [update: I can't find it in the documentation], so the graphics context is exactly the same either way? I also tried to step into UIGraphicsBeginImageContext
but the debugger won't let me step into it.

- 146,324
- 131
- 460
- 740
-
Do you doubt the documentation? If so, why? – borrrden May 31 '12 at 08:12
-
My question really is... does it use `CGBitmapContextCreate`, if this was ever documented? – nonopolarity May 31 '12 at 08:28
1 Answers
In the UIKit Function References page of iOS documentation, the following is written about UIGraphicsBeginImageContext
:
Creates a bitmap-based graphics context and makes it the current context.
Emphasis added. Following a link to the CGContextRef page, I find this:
A graphics context contains drawing parameters and all device-specific information needed to render the paint on a page to the destination, whether the destination is a window in an application, a bitmap image, a PDF document, or a printer.
Again, emphasis added. This says that (as of now) there are 4 kinds of Core Graphics contexts, each with their own initializers. The only kind that has to do with bitmaps is a bitmap-based CGContextRef, and there is only one documented way to create them (well, technically it comes in two versions). It is very likely that this function is being used. I believe that UIGraphicsBeginImageContext
is merely a convenience method. It just sets up a default set of parameters for CGBitmapContextCreate (it takes a lot of parameters) and pushes the created context onto the graphics stack.

- 33,256
- 8
- 74
- 109
-
the reason is that there are also `CGLayer` graphics context, and `CALayer` graphics context. (note `CGLayer` vs `CALayer`). Some are said to be able to be cached on the GPU, some are not... so they are quite complicated... – nonopolarity May 31 '12 at 09:36
-
Layers are not the same as contexts. Layers store data about how to render things into contexts. As stated above, there are only four kinds of contexts right now. – borrrden May 31 '12 at 11:08
-
-
I'm aware of that, but you need to create a CGLayer with a context first, which has to be one of those 4 kinds. The CGLayer is then optimized for that kind of context. It is mostly used for offscreen rendering (according to the docs) so it doesn't really make sense to make one with UIGraphicsBeginImageContext() because the purpose of that is just to render something once. – borrrden May 31 '12 at 14:27
-
"according to the docs"? where? in the Quartz 2D Programming Guide? It says it can be used for offline, but it doesn't say it is mostly used for offscreen. The docs mentioned "All layer drawing starts with a graphics context from which you create a CGLayer object using the function CGLayerCreateWithContext. The graphics context used to create a CGLayer object is typically a window graphics context.", so it seems that typically, it is for something that would show on the screen – nonopolarity May 31 '12 at 21:46
-
CGLayer reference says (was wrong about the wording) is "useful" for offscreen drawing: http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Reference/CGLayer/Reference/reference.html#//apple_ref/doc/uid/TP40001406 – borrrden May 31 '12 at 23:43
-
As an update, I just finished watching a WWDC session about Core Graphics, and the speaker stated that you should use CGBitmapContextCreate only if your algorithm needs access to the underlying byte data. Otherwise, use UIGraphicsBeginImageContext. This implies that they are the same (to me anyway). – borrrden Jun 22 '12 at 16:36