Class

NSGraphicsContext

The NSGraphicsContext class is the programmatic interface to objects that represent graphics contexts. A context can be thought of as a destination to which drawing and graphics state operations are sent for execution. Each graphics context contains its own graphics environment and state.

Overview

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 an NSGraphicsContext object you remove it from the stack, and the next graphics context on the stack becomes the current graphics context.

Symbols

Creating a Graphics Context

init?(attributes: [String : 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.

Deprecated
init(window: NSWindow)

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

Managing the Current Context

class func current()

Returns the current graphics context of the current thread.

class func setCurrent(NSGraphicsContext?)

Sets 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.

Deprecated

Managing the Graphics State

class func setGraphicsState(Int)

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

Deprecated
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: [String : 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.

Constants

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.

NSImageInterpolation

These interpolations are used by imageInterpolation property.

NSColorRenderingIntent

These constants specify how Cocoa should handle colors that are not located within the destination color space of a graphics context. These constants are used by the property colorRenderingIntent.

Relationships

Inherits From

Conforms To