CGContext

A Quartz 2D drawing destination.

Overview

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. You can obtain a graphics context by using Quartz graphics context creation functions or by using higher-level functions provided in the Carbon, Cocoa, or Printing frameworks. Quartz provides creation functions for various flavors of Quartz graphics contexts including bitmap images and PDF. The Cocoa framework provides functions for obtaining window graphics contexts. The Printing framework provides functions that obtain a graphics context appropriate for the destination printer.

Topics

Managing Graphics Contexts

CGContextFlush

Forces all pending drawing operations in a window context to be rendered immediately to the destination device.

CGContextGetTypeID

Returns the type identifier for a graphics context.

CGContextRelease

Decrements the retain count of a graphics context.

CGContextRetain

Increments the retain count of a graphics context.

CGContextSynchronize

Marks a window context for update.

Saving and Restoring the Current Graphics State

CGContextSaveGState

Pushes a copy of the current graphics state onto the graphics state stack for the context.

CGContextRestoreGState

Sets the current graphics state to the state most recently saved.

Getting and Setting Graphics State Parameters

CGContextGetInterpolationQuality

Returns the current level of interpolation quality for a graphics context.

CGContextSetFlatness

Sets the accuracy of curved paths in a graphics context.

CGContextSetInterpolationQuality

Sets the level of interpolation quality for a graphics context.

CGContextSetLineCap

Sets the style for the endpoints of lines drawn in a graphics context.

CGContextSetLineDash

Sets the pattern for dashed lines in a graphics context.

CGContextSetLineJoin

Sets the style for the joins of connected lines in a graphics context.

CGContextSetLineWidth

Sets the line width for a graphics context.

CGContextSetMiterLimit

Sets the miter limit for the joins of connected lines in a graphics context.

CGContextSetPatternPhase

Sets the pattern phase of a context.

CGContextSetFillPattern

Sets the fill pattern in the specified graphics context.

CGContextSetRenderingIntent

Sets the rendering intent in the current graphics state.

CGContextSetShouldAntialias

Sets antialiasing on or off for a graphics context.

CGContextSetStrokePattern

Sets the stroke pattern in the specified graphics context.

CGContextSetBlendMode

Sets how sample values are composited by a graphics context.

CGContextSetAllowsAntialiasing

Sets whether or not to allow antialiasing for a graphics context.

CGContextSetAllowsFontSmoothing

Sets whether or not to allow font smoothing for a graphics context.

CGContextSetShouldSmoothFonts

Enables or disables font smoothing in a graphics context.

CGContextSetAllowsFontSubpixelPositioning

Sets whether or not to allow subpixel positioning for a graphics context.

CGContextSetShouldSubpixelPositionFonts

Enables or disables subpixel positioning in a graphics context.

CGContextSetAllowsFontSubpixelQuantization

Sets whether or not to allow subpixel quantization for a graphics context.

CGContextSetShouldSubpixelQuantizeFonts

Enables or disables subpixel quantization in a graphics context.

Constructing Paths

These functions are used to define the geometry of the current path. For more information on how paths are defined, see CGPath.

CGContextAddArc

Adds an arc of a circle to the current path, possibly preceded by a straight line segment

CGContextAddArcToPoint

Adds an arc of a circle to the current path, using a radius and tangent points.

CGContextAddCurveToPoint

Appends a cubic Bézier curve from the current point, using the provided control points and end point .

CGContextAddLines

Adds a sequence of connected straight-line segments to the current path.

CGContextAddLineToPoint

Appends a straight line segment from the current point to the provided point .

CGContextAddPath

Adds a previously created path object to the current path in a graphics context.

CGContextCopyPath

Returns a path object built from the current path information in a graphics context.

CGContextAddQuadCurveToPoint

Appends a quadratic Bézier curve from the current point, using a control point and an end point you specify.

CGContextAddRect

Adds a rectangular path to the current path.

CGContextAddRects

Adds a set of rectangular paths to the current path.

CGContextBeginPath

Creates a new empty path in a graphics context.

CGContextClosePath

Closes and terminates the current path’s subpath.

CGContextMoveToPoint

Begins a new subpath at the point you specify.

CGContextAddEllipseInRect

Adds an ellipse that fits inside the specified rectangle.

Painting Paths

These functions are used to stroke along or fill in the current path.

CGContextClearRect

Paints a transparent rectangle.

CGContextDrawPath

Draws the current path using the provided drawing mode.

CGContextEOFillPath

Paints the area within the current path, using the even-odd fill rule.

CGContextFillPath

Paints the area within the current path, using the nonzero winding number rule.

CGContextFillRect

Paints the area contained within the provided rectangle, using the fill color in the current graphics state.

CGContextFillRects

Paints the areas contained within the provided rectangles, using the fill color in the current graphics state.

CGContextFillEllipseInRect

Paints the area of the ellipse that fits inside the provided rectangle, using the fill color in the current graphics state.

CGContextStrokePath

Paints a line along the current path.

CGContextStrokeRect

Paints a rectangular path.

CGContextStrokeRectWithWidth

Paints a rectangular path, using the specified line width.

CGContextReplacePathWithStrokedPath

Replaces the path in the graphics context with the stroked version of the path.

CGContextStrokeEllipseInRect

Strokes an ellipse that fits inside the specified rectangle.

CGContextStrokeLineSegments

Strokes a sequence of line segments.

Getting Information About Paths

CGContextIsPathEmpty

Indicates whether the current path contains any subpaths.

CGContextGetPathCurrentPoint

Returns the current point in a non-empty path.

CGContextGetPathBoundingBox

Returns the smallest rectangle that contains the current path.

CGContextPathContainsPoint

Checks to see whether the specified point is contained in the current path.

Modifying Clipping Paths

CGContextClip

Modifies the current clipping path, using the nonzero winding number rule.

CGContextEOClip

Modifies the current clipping path, using the even-odd rule.

CGContextClipToRect

Sets the clipping path to the intersection of the current clipping path with the area defined by the specified rectangle.

CGContextClipToRects

Sets the clipping path to the intersection of the current clipping path with the region defined by an array of rectangles.

CGContextGetClipBoundingBox

Returns the bounding box of a clipping path.

CGContextClipToMask

Maps a mask into the specified rectangle and intersects it with the current clipping area of the graphics context.

Setting Color, Color Space, and Shadow Values

CGContextSetAlpha

Sets the opacity level for objects drawn in a graphics context.

CGContextSetCMYKFillColor

Sets the current fill color to a value in the DeviceCMYK color space.

CGContextSetFillColor

Sets the current fill color.

CGContextSetCMYKStrokeColor

Sets the current stroke color to a value in the DeviceCMYK color space.

CGContextSetFillColorSpace

Sets the fill color space in a graphics context.

CGContextSetFillColorWithColor

Sets the current fill color in a graphics context, using a CGColor.

CGContextSetGrayFillColor

Sets the current fill color to a value in the DeviceGray color space.

CGContextSetGrayStrokeColor

Sets the current stroke color to a value in the DeviceGray color space.

CGContextSetRGBFillColor

Sets the current fill color to a value in the DeviceRGB color space.

CGContextSetRGBStrokeColor

Sets the current stroke color to a value in the DeviceRGB color space.

CGContextSetShadow

Enables shadowing in a graphics context.

CGContextSetShadowWithColor

Enables shadowing with color a graphics context.

CGContextSetStrokeColor

Sets the current stroke color.

CGContextSetStrokeColorSpace

Sets the stroke color space in a graphics context.

CGContextSetStrokeColorWithColor

Sets the current stroke color in a context, using a CGColor.

Transforming User Space

These functions allow you to examine and change the current transformation matrix (CTM) in a graphics context.

CGContextConcatCTM

Transforms the user coordinate system in a context using a specified matrix.

CGContextGetCTM

Returns the current transformation matrix.

CGContextRotateCTM

Rotates the user coordinate system in a context.

CGContextScaleCTM

Changes the scale of the user coordinate system in a context.

CGContextTranslateCTM

Changes the origin of the user coordinate system in a context.

Using Transparency Layers

CGContextBeginTransparencyLayer

Begins a transparency layer.

CGContextBeginTransparencyLayerWithRect

Begins a transparency layer whose contents are bounded by the specified rectangle.

CGContextEndTransparencyLayer

Ends a transparency layer.

Drawing an Image to a Graphics Context

CGContextDrawTiledImage

Repeatedly draws an image, scaled to the provided rectangle, to fill the current clip region.

CGContextDrawImage

Draws an image into a graphics context.

Drawing PDF Content to a Graphics Context

CGContextDrawPDFPage

Draws the content of a PDF page into the current graphics context.

Drawing With a Gradient

CGContextDrawLinearGradient

Paints a gradient fill that varies along the line defined by the provided starting and ending points.

CGContextDrawRadialGradient

Paints a gradient fill that varies along the area defined by the provided starting and ending circles.

Drawing With a Shading

CGContextDrawShading

Fills the clipping path of a context with the specified shading.

Setting Up a Page-Based Graphics Context

CGContextBeginPage

Starts a new page in a page-based graphics context.

CGContextEndPage

Ends the current page in a page-based graphics context.

Drawing Glyphs

CGContextShowGlyphs

Displays an array of glyphs at the current text position.

Deprecated
CGContextShowGlyphsAtPoint

Displays an array of glyphs at a position you specify.

Deprecated
CGContextShowGlyphsWithAdvances

Draws an array of glyphs with varying offsets.

Deprecated
CGContextShowGlyphsAtPositions

Draws glyphs at the provided position.

Drawing Text

CGContextGetTextMatrix

Returns the current text matrix.

CGContextGetTextPosition

Returns the location at which text is drawn.

CGContextSelectFont

Sets the font and font size in a graphics context.

Deprecated
CGContextSetCharacterSpacing

Sets the current character spacing.

CGContextSetFont

Sets the platform font in a graphics context.

CGContextSetFontSize

Sets the current font size.

CGContextSetTextDrawingMode

Sets the current text drawing mode.

CGContextSetTextMatrix

Sets the current text matrix.

CGContextSetTextPosition

Sets the location at which text is drawn.

CGContextShowText

Displays a character array at the current text position, a point specified by the current text matrix.

Deprecated
CGContextShowTextAtPoint

Displays a character string at a position you specify.

Deprecated

Converting Between Device Space and User Space

CGContextGetUserSpaceToDeviceSpaceTransform

Returns an affine transform that maps user space coordinates to device space coordinates.

CGContextConvertPointToDeviceSpace

Returns a point that is transformed from user space coordinates to device space coordinates.

CGContextConvertPointToUserSpace

Returns a point that is transformed from device space coordinates to user space coordinates.

CGContextConvertSizeToDeviceSpace

Returns a size that is transformed from user space coordinates to device space coordinates.

CGContextConvertSizeToUserSpace

Returns a size that is transformed from device space coordinates to user space coordinates.

CGContextConvertRectToDeviceSpace

Returns a rectangle that is transformed from user space coordinate to device space coordinates.

CGContextConvertRectToUserSpace

Returns a rectangle that is transformed from device space coordinate to user space coordinates.

Data Types

CGContextRef

A Quartz 2D drawing environment.

Constants

CGBlendMode

Compositing operations for images.

CGInterpolationQuality

Levels of interpolation quality for rendering an image.

CGTextDrawingMode

Modes for rendering text.

CGTextEncoding

Text encodings for fonts.

Deprecated

See Also

Opaque Types

CGBitmapContext

A type of CGContextRef that you can use for drawing bits to memory.

CGColor

A set of components that define a color, with a color space specifying how to interpret them.

CGColorConversionInfo

An object that describes how to convert between color spaces for use by other system services.

CGColorSpace

A profile that specifies how to interpret a color value for display.

CGDataConsumer

An abstraction for data-writing tasks that eliminates the need to manage a raw memory buffer.

CGDataProvider

An abstraction for data-reading tasks that eliminates the need to manage a raw memory buffer.

CGFont

A set of character glyphs and layout information for drawing text.

CGFunction

A general facility for defining and using callback functions.

CGGLContext

Deprecated. A type of CGContextRef for drawing OpenGL content.

CGGradient

A definition for a smooth transition between colors for drawing radial and axial gradient fills.

CGImage

A bitmap image or image mask.

CGLayer

An offscreen context for reusing content drawn with Core Graphics.

CGPath

Immutable and mutable types for graphics paths: mathematical descriptions of shapes or lines to be drawn in a graphics context.

CGPattern

A 2D pattern to be used for drawing graphics paths.

CGPDFArray

An array structure within a PDF document.

CGPDFContentStream

A representation of one or more content data streams in a PDF page.

CGPDFContext

A type of CGContextRef for drawing PDF content.

CGPDFDictionary

A dictionary structure within a PDF document.

CGPDFDocument

A document that contains PDF (Portable Document Format) drawing information.

CGPDFObject

An object representing content within a PDF document.

CGPDFOperatorTable

A set of callback functions for operators used when scanning content in a PDF document.

CGPDFPage

A page in a PDF document.

CGPDFScanner

A parser object for handling content and operators in a PDF content stream.

CGPDFStream

A stream or sequence of data bytes in a PDF document.

CGPDFString

A text string in a PDF document.

CGPSConverter

An object that converts PostScript data to PDF data.

CGShading

A definition for a smooth transition between colors, controlled by a custom function you provide, for drawing radial and axial gradient fills.