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.

Symbols

Managing Graphics Contexts

func flush()

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

class var typeID: CFTypeID

Returns the type identifier for a graphics context.

func synchronize()

Marks a window context for update.

Saving and Restoring the Current Graphics State

func saveGState()

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

func restoreGState()

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

Getting and Setting Graphics State Parameters

var interpolationQuality: CGInterpolationQuality

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

func setFlatness(CGFloat)

Sets the accuracy of curved paths in a graphics context.

func setLineCap(CGLineCap)

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

func setLineJoin(CGLineJoin)

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

func setLineWidth(CGFloat)

Sets the line width for a graphics context.

func setMiterLimit(CGFloat)

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

func setPatternPhase(CGSize)

Sets the pattern phase of a context.

func setFillPattern(CGPattern, colorComponents: UnsafePointer<CGFloat>)

Sets the fill pattern in the specified graphics context.

func setRenderingIntent(CGColorRenderingIntent)

Sets the rendering intent in the current graphics state.

func setShouldAntialias(Bool)

Sets anti-aliasing on or off for a graphics context.

func setStrokePattern(CGPattern, colorComponents: UnsafePointer<CGFloat>)

Sets the stroke pattern in the specified graphics context.

func setBlendMode(CGBlendMode)

Sets how sample values are composited by a graphics context.

func setAllowsAntialiasing(Bool)

Sets whether or not to allow anti-aliasing for a graphics context.

func setAllowsFontSmoothing(Bool)

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

func setShouldSmoothFonts(Bool)

Enables or disables font smoothing in a graphics context.

func setAllowsFontSubpixelPositioning(Bool)

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

func setShouldSubpixelPositionFonts(Bool)

Enables or disables subpixel positioning in a graphics context.

func setAllowsFontSubpixelQuantization(Bool)

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

func setShouldSubpixelQuantizeFonts(Bool)

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.

func addPath(CGPath)

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

var path: CGPath?

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

func addRect(CGRect)

Adds a rectangular path to the current path.

func beginPath()

Creates a new empty path in a graphics context.

func closePath()

Closes and terminates the current path’s subpath.

func addEllipse(in: CGRect)

Adds an ellipse that fits inside the specified rectangle.

Painting Paths

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

func clear(CGRect)

Paints a transparent rectangle.

func drawPath(using: CGPathDrawingMode)

Draws the current path using the provided drawing mode.

func fill(CGRect)

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

func fillEllipse(in: CGRect)

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

func strokePath()

Paints a line along the current path.

func stroke(CGRect)

Paints a rectangular path.

func stroke(CGRect, width: CGFloat)

Paints a rectangular path, using the specified line width.

func replacePathWithStrokedPath()

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

func strokeEllipse(in: CGRect)

Strokes an ellipse that fits inside the specified rectangle.

Getting Information About Paths

var isPathEmpty: Bool

Indicates whether the current path contains any subpaths.

var currentPointOfPath: CGPoint

Returns the current point in a non-empty path.

var boundingBoxOfPath: CGRect

Returns the smallest rectangle that contains the current path.

func pathContains(CGPoint, mode: CGPathDrawingMode)

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

Modifying Clipping Paths

func clip(to: CGRect)

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

var boundingBoxOfClipPath: CGRect

Returns the bounding box of a clipping path.

func clip(to: CGRect, mask: CGImage)

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

func setAlpha(CGFloat)

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

func setFillColor(cyan: CGFloat, magenta: CGFloat, yellow: CGFloat, black: CGFloat, alpha: CGFloat)

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

func setFillColor(UnsafePointer<CGFloat>)

Sets the current fill color.

func setStrokeColor(cyan: CGFloat, magenta: CGFloat, yellow: CGFloat, black: CGFloat, alpha: CGFloat)

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

func setFillColorSpace(CGColorSpace)

Sets the fill color space in a graphics context.

func setFillColor(CGColor)

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

func setFillColor(gray: CGFloat, alpha: CGFloat)

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

func setStrokeColor(gray: CGFloat, alpha: CGFloat)

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

func setFillColor(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)

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

func setStrokeColor(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)

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

func setShadow(offset: CGSize, blur: CGFloat)

Enables shadowing in a graphics context.

func setShadow(offset: CGSize, blur: CGFloat, color: CGColor?)

Enables shadowing with color a graphics context.

func setStrokeColor(UnsafePointer<CGFloat>)

Sets the current stroke color.

func setStrokeColorSpace(CGColorSpace)

Sets the stroke color space in a graphics context.

func setStrokeColor(CGColor)

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.

func concatenate(CGAffineTransform)

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

var ctm: CGAffineTransform

Returns the current transformation matrix.

func rotate(by: CGFloat)

Rotates the user coordinate system in a context.

func scaleBy(x: CGFloat, y: CGFloat)

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

func translateBy(x: CGFloat, y: CGFloat)

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

Using Transparency Layers

func beginTransparencyLayer(in: CGRect, auxiliaryInfo: CFDictionary?)

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

func endTransparencyLayer()

Ends a transparency layer.

Drawing PDF Content to a Graphics Context

func drawPDFPage(CGPDFPage)

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

Drawing With a Gradient

func drawLinearGradient(CGGradient, start: CGPoint, end: CGPoint, options: CGGradientDrawingOptions)

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

Drawing With a Shading

func drawShading(CGShading)

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

Setting Up a Page-Based Graphics Context

func beginPage(mediaBox: UnsafePointer<CGRect>?)

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

func endPage()

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

Drawing Glyphs

func showGlyphs(g: UnsafePointer<CGGlyph>?, count: Int)

Displays an array of glyphs at the current text position.

Deprecated
func showGlyphsAtPoint(x: CGFloat, y: CGFloat, glyphs: UnsafePointer<CGGlyph>?, count: Int)

Displays an array of glyphs at a position you specify.

Deprecated

Drawing Text

var textMatrix: CGAffineTransform

Returns the current text matrix.

var textPosition: CGPoint

Returns the location at which text is drawn.

func selectFont(name: UnsafePointer<Int8>, size: CGFloat, textEncoding: CGTextEncoding)

Sets the font and font size in a graphics context.

Deprecated
func setCharacterSpacing(CGFloat)

Sets the current character spacing.

func setFont(CGFont)

Sets the platform font in a graphics context.

func setFontSize(CGFloat)

Sets the current font size.

func setTextDrawingMode(CGTextDrawingMode)

Sets the current text drawing mode.

func showText(string: UnsafePointer<Int8>, length: Int)

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

Deprecated
func showTextAtPoint(x: CGFloat, y: CGFloat, string: UnsafePointer<Int8>, length: Int)

Displays a character string at a position you specify.

Deprecated

Converting Between Device Space and User Space

var userSpaceToDeviceSpaceTransform: CGAffineTransform

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

func convertToDeviceSpace(CGPoint)

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

func convertToUserSpace(CGPoint)

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

func convertToDeviceSpace(CGSize)

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

func convertToUserSpace(CGSize)

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

func convertToDeviceSpace(CGRect)

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

func convertToUserSpace(CGRect)

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

Data Types

CGContext

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