Drawing to a View Graphics Context in iOS
To draw to the screen in an iOS application, you set up a
UIView
object and implement its
drawRect:
method to perform drawing. You obtain graphics context in your
drawRect:
method by calling the UIKit function
UIGraphicsGetCurrentContext
.
The default coordinate system used throughout UIKit is different from the coordinate system used by Quartz. In UIKit, the origin is in the upper-left corner, with the positive-y value pointing downward. The
UIView
object modifies the CTM of the Quartz graphics context to match the UIKit conventions by translating the origin to the upper left corner of the view and inverting the y-axis by multiplying it by
-1
.
Creating a PDF Graphics Context
The Quartz 2D API provides two functions that create a PDF graphics context:
-
, which you use when you want to specify the location for the PDF output as a Core Foundation URL.CGPDFContextCreateWithURL
-
, which you use when you want the PDF output sent to a data consumer.CGPDFContextCreate
iOS Note: A PDF graphics context in iOS uses the default coordinate system provided by Quartz, without applying a transform to match the UIKit coordinate system.
Creating a Bitmap Graphics Context
Note: Bitmap graphics contexts are sometimes used for drawing offscreen.
iOS Note: iOS applications should use the function
UIGraphicsBeginImageContextWithOptions
instead of using the low-level Quartz functions described here. If your application creates an offscreen bitmap using Quartz, the coordinate system used by bitmap graphics context is the default Quartz coordinate system. if your application creates an image context by calling the function
UIGraphicsBeginImageContextWithOptions
, UIKit applies the same transformation to the context’s coordinate system as it does to a
UIView
object’s graphics context.
You use the function
CGBitmapContextCreate
to create a bitmap graphics context.
Anti-Aliasing
You can turn anti-aliasing off for a particular bitmap graphics context by calling the function
CGContextSetShouldAntialias
.You can control whether to allow anti-aliasing for a particular graphics context by using the function
CGContextSetAllowsAntialiasing
. Pass
true
to this function to allow anti-aliasing;
false
not to allow it.