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. You can imagine it as some sort of C struct (though whether it is or not is irrelevant - it might be a C++ object, an Objective-C Object, or something else). It might look something like this:
struct CGBitmapContext {
void* bitmapData;
UInt32 rowBytes;
float currentTransform [4][4];
float lineWidth;
CGColorRef fillColor;
CGColorRef strokeColor;
// ... other graphics state needed for drawing ...
};
When you issue commands to draw, such as calling CGContextFillRect()
, it uses that state to decide how and where to draw. It will transform the rectangle you passed in using the current transformation matrix, then fill it in with the fill color.
But you don't deal directly with a CGContext
. Instead, you have a CGContextRef
, which is a pointer to the above struct (or object, or whatever it really is). So the context resides at the address pointed to by the CGContextRef
. When you call CGContextRelease()
, if the retain count for the context is 0, then the memory for the struct or object or whatever, is freed, probably using free()
, or delete
or -release
.
You can have as many contexts of any type as will fit in memory. They are all independent. If you are using bitmap contexts, freeing the context does not necessarily free the bitmap data. (I believe it depends on how you created the context.) You can use the data from various contexts to blend together, if you want. You could make CGImages out of them if you want, or upload them to the video card for use with OpenGL. You can do whatever you want with them.
In the days of MacOS 9, this was much more confusing because there were not contexts. You had to set the state every time you wanted to draw. And if you weren't careful, a previous function may have set the state to something odd. If you didn't notice the state had changed, and didn't change it back to what you wanted, the drawing would look bad. With a context, you can save the state and restore it using CGContextSaveGState()
and CGContextRestoreGState()
, and you can use different contexts for drawing to different views within the same window, for example. This greatly simplifies drawing.