<!--
{
  "availability" : [
    "macOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSGraphicsContext",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSGraphicsContext"
  },
  "title" : "NSGraphicsContext"
}
-->

# NSGraphicsContext

An object that represents a graphics context.

```
class NSGraphicsContext
```

## Overview

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`](/documentation/AppKit/NSGraphicsContext) class is an abstract superclass for destination-specific graphics contexts. You obtain instances of concrete subclasses with the class methods [`current`](/documentation/AppKit/NSGraphicsContext/current), [`init(attributes:)`](/documentation/AppKit/NSGraphicsContext/init(attributes:)), [`init(bitmapImageRep:)`](/documentation/AppKit/NSGraphicsContext/init(bitmapImageRep:)), [`init(cgContext:flipped:)`](/documentation/AppKit/NSGraphicsContext/init(cgContext:flipped:)-9cbad), and [`init(window:)`](/documentation/AppKit/NSGraphicsContext/init(window:)).

At any time there is the notion of the current context. The current context for the current thread may be set using [`current`](/documentation/AppKit/NSGraphicsContext/current).

Graphics contexts are maintained on a stack. You push a graphics context onto the stack by sending it a [`saveGraphicsState()`](/documentation/AppKit/NSGraphicsContext/saveGraphicsState()-swift.method) message, and pop it off the stack by sending it a [`restoreGraphicsState()`](/documentation/AppKit/NSGraphicsContext/restoreGraphicsState()-swift.method) message. By sending [`restoreGraphicsState()`](/documentation/AppKit/NSGraphicsContext/restoreGraphicsState()-swift.method) to a graphics context object you remove it from the stack, and the next graphics context on the stack becomes the current graphics context.

## Topics

### Creating a Graphics Context

[`init(attributes:)`](/documentation/AppKit/NSGraphicsContext/init(attributes:))

Creates a graphics context using the specified attributes.

[`init(bitmapImageRep:)`](/documentation/AppKit/NSGraphicsContext/init(bitmapImageRep:))

Creates a new graphics context using the specified bitmap image representation object as the context destination.

[`init(cgContext:flipped:)`](/documentation/AppKit/NSGraphicsContext/init(cgContext:flipped:)-9cbad)

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

[`init(window:)`](/documentation/AppKit/NSGraphicsContext/init(window:))

Creates a new graphics context for drawing into a window.

[`init(graphicsPort:flipped:)`](/documentation/AppKit/NSGraphicsContext/init(graphicsPort:flipped:))

Creates a new graphics context from the specified graphics port.

### Managing the Current Context

[`current`](/documentation/AppKit/NSGraphicsContext/current)

Returns the current graphics context of the current thread.

[`cgContext`](/documentation/AppKit/NSGraphicsContext/cgContext)

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

[`graphicsPort`](/documentation/AppKit/NSGraphicsContext/graphicsPort)

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

### Managing the Graphics State

[`restoreGraphicsState()`](/documentation/AppKit/NSGraphicsContext/restoreGraphicsState()-swift.type.method)

Pops a graphics context from the per-thread stack, makes it current, and sends the context a restore graphics state message.

[`restoreGraphicsState()`](/documentation/AppKit/NSGraphicsContext/restoreGraphicsState()-swift.method)

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

[`saveGraphicsState()`](/documentation/AppKit/NSGraphicsContext/saveGraphicsState()-swift.type.method)

Saves the graphics state of the current graphics context.

[`saveGraphicsState()`](/documentation/AppKit/NSGraphicsContext/saveGraphicsState()-swift.method)

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

[`setGraphicsState(_:)`](/documentation/AppKit/NSGraphicsContext/setGraphicsState(_:))

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

### Testing the Drawing Destination

[`currentContextDrawingToScreen()`](/documentation/AppKit/NSGraphicsContext/currentContextDrawingToScreen())

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

[`isDrawingToScreen`](/documentation/AppKit/NSGraphicsContext/isDrawingToScreen)

A Boolean value that indicates whether the drawing destination is the screen.

### Getting Information About the Context

[`attributes`](/documentation/AppKit/NSGraphicsContext/attributes)

The attributes used to create this instance.

[`NSGraphicsContext.AttributeKey`](/documentation/AppKit/NSGraphicsContext/AttributeKey)

Constants that specify the dictionary keys for the attributes of the graphics context.

[`NSGraphicsContext.RepresentationFormatName`](/documentation/AppKit/NSGraphicsContext/RepresentationFormatName)

Constants that specify values for the representation format name key in a graphic context’s attributes dictionary.

[`isFlipped`](/documentation/AppKit/NSGraphicsContext/isFlipped)

A Boolean value that indicates the graphics context’s flipped state.

### Flushing Graphics to the Context

[`flushGraphics()`](/documentation/AppKit/NSGraphicsContext/flushGraphics())

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

### Managing the Focus Stack

[`focusStack`](/documentation/AppKit/NSGraphicsContext/focusStack)

Returns the object used by the context to track the hierarchy of views with locked focus.

[`setFocusStack:`](/documentation/AppKit/NSGraphicsContext/setFocusStack:)

Sets the object used by the receiver to track the hierarchy of views with locked focus.

### Configuring Rendering Options

[`compositingOperation`](/documentation/AppKit/NSGraphicsContext/compositingOperation)

The graphics context’s global compositing operation setting.

[`NSCompositingOperation`](/documentation/AppKit/NSCompositingOperation)

Constants that describe compositing operators in terms of source and destination images, each having an opaque and transparent region.

[`imageInterpolation`](/documentation/AppKit/NSGraphicsContext/imageInterpolation)

A constant that specifies the graphics context’s interpolation, or image smoothing, behavior.

[`NSImageInterpolation`](/documentation/AppKit/NSImageInterpolation)

Constants that specify the interpolation, or image smoothing, behavior used by the image interpolation property.

[`shouldAntialias`](/documentation/AppKit/NSGraphicsContext/shouldAntialias)

A Boolean value that indicates whether the graphics context uses antialiasing.

[`patternPhase`](/documentation/AppKit/NSGraphicsContext/patternPhase)

The amount to offset the pattern color when filling the graphics context.

### Getting the Context for Rendering Core Image Objects

[`ciContext`](/documentation/AppKit/NSGraphicsContext/ciContext)

A context for Core Image objects that you can use to render into the graphics context.

### Managing Color Rendering

[`colorRenderingIntent`](/documentation/AppKit/NSGraphicsContext/colorRenderingIntent)

The color rendering intent in the graphics context’s graphics state.

[`NSColorRenderingIntent`](/documentation/AppKit/NSColorRenderingIntent)

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



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)