An object that represents a graphics context.


You can think of a graphics context as a destination to which drawing and graphics state operations are sent for execution. Each graphics context contains its own graphics environment and state.

The NSGraphicsContext class is an abstract superclass for destination-specific graphics contexts. You obtain instances of concrete subclasses with the class methods current, init(attributes:), init(bitmapImageRep:), init(cgContext:flipped:), and init(window:).

At any time there is the notion of the current context. The current context for the current thread may be set using setCurrent(_:).

Graphics contexts are maintained on a stack. You push a graphics context onto the stack by sending it a saveGraphicsState() message, and pop it off the stack by sending it a restoreGraphicsState() message. By sending restoreGraphicsState() to a graphics context object you remove it from the stack, and the next graphics context on the stack becomes the current graphics context.


Creating a Graphics Context

init?(attributes: [NSGraphicsContext.AttributeKey : Any] = [:])

Instantiates and returns an instance of NSGraphicsContext using the specified attributes.

init?(bitmapImageRep: NSBitmapImageRep)

Instantiates and returns a new graphics context using the supplied NSBitmapImageRep object as the context destination.

init(cgContext: CGContext, flipped: Bool)

Creates a new graphics context from the specified Core Graphics context and the initial flipped state.

init(graphicsPort: UnsafeMutableRawPointer, flipped: Bool)

Instantiates and returns a new graphics context from the given graphics port.

init(window: NSWindow)

Creates and returns a new graphics context for drawing into a window.

Managing the Current Context

class var current: NSGraphicsContext?

Returns the current graphics context of the current thread.

var cgContext: CGContext

The Core Graphics context, which is a low-level, platform-specific graphics context.

var graphicsPort: UnsafeMutableRawPointer

The low-level, platform-specific graphics context represented by the graphic port.


Managing the Graphics State

class func setGraphicsState(Int)

Makes the graphics context of the specified graphics state current, and resets graphics state.

class func restoreGraphicsState()

Pops a graphics context from the per-thread stack, makes it current, and sends the context a restoreGraphicsState() message.

func restoreGraphicsState()

Removes the receiver’s graphics state from the top of the graphics state stack and makes the next graphics state the current graphics state.

class func saveGraphicsState()

Saves the graphics state of the current graphics context.

func saveGraphicsState()

Saves the current graphics state and creates a new graphics state on the top of the stack.

Testing the Drawing Destination

class func currentContextDrawingToScreen()

Returns a Boolean value that indicates whether the current context is drawing to the screen.

var isDrawingToScreen: Bool

A Boolean value indicating whether the drawing destination is the screen.

Getting Information About a Context

var attributes: [NSGraphicsContext.AttributeKey : Any]?

The attributes used to create this instance.

var isFlipped: Bool

A Boolean value indicating the receiver’s flipped state.

Flushing Graphics to the Context

func flushGraphics()

Forces any buffered operations or data to be sent to the receiver’s destination.

Configuring Rendering Options

var compositingOperation: NSCompositingOperation

The receiver’s global compositing operation setting.

var imageInterpolation: NSImageInterpolation

A constant that specifies the receiver’s interpolation (image smoothing) behavior.

var shouldAntialias: Bool

A Boolean value indicating whether the receiver uses antialiasing.

var patternPhase: NSPoint

The amount to offset the pattern color when filling the receiver.

Getting the Core Image Context

var ciContext: CIContext?

A CIContext object that you can use to render into the receiver.

Managing the Color Rendering Intent

var colorRenderingIntent: NSColorRenderingIntent

The current rendering intent in the receiver’s graphics state.


Attribute dictionary keys

These constants are dictionary keys used by init(attributes:) and attributes.

Representation format attribute keys

These constants are possible values for the NSGraphicsContextRepresentationFormatAttributeName key in a graphic context’s attribute dictionary.

enum NSImageInterpolation

Interpolations used by the imageInterpolation property.

enum NSColorRenderingIntent

Constants that specify how Cocoa should handle colors that are not located within the destination color space of a graphics context.


Inherits From

Conforms To

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software