<!--
{
  "availability" : [
    "iOS: 5.0.0 -",
    "iPadOS: 5.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.4.0 -",
    "tvOS: -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "CoreImage",
  "identifier" : "/documentation/CoreImage/CIContext",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Core Image"
    ],
    "preciseIdentifier" : "c:objc(cs)CIContext"
  },
  "title" : "CIContext"
}
-->

# CIContext

The Core Image context class provides an evaluation context for Core Image processing with Metal, OpenGL, or OpenCL.

```
class CIContext
```

## Overview

You use a `CIContext` instance to render a [`CIImage`](/documentation/CoreImage/CIImage) instance which represents a graph of image processing operations
which are built using other Core Image classes, such as [`CIFilter`](/documentation/CoreImage/CIFilter-swift.class), [`CIKernel`](/documentation/CoreImage/CIKernel), [`CIColor`](/documentation/CoreImage/CIColor) and [`CIImage`](/documentation/CoreImage/CIImage).
You can also use a `CIContext` with the [`CIDetector`](/documentation/CoreImage/CIDetector) class to analyze images — for example, to detect faces
or barcodes.

Contexts support automatic color management by performing all processing operations in a working color space.
This means that unless told otherwise:

- All input images are color matched from the input’s color space to the working space.
- All renders are color matched from the working space to the destination space.
  (For more information on `CGColorSpace` see <doc://com.apple.documentation/documentation/CoreGraphics/CGColorSpace>)

`CIContext` and [`CIImage`](/documentation/CoreImage/CIImage) instances are immutable, so multiple threads can use the same [`CIContext`](/documentation/CoreImage/CIContext) instance
to render [`CIImage`](/documentation/CoreImage/CIImage) instances. However, [`CIFilter`](/documentation/CoreImage/CIFilter-swift.class) instances are mutable and thus cannot be shared safely among
threads. Each thread must take case not to access or modify a [`CIFilter`](/documentation/CoreImage/CIFilter-swift.class) instance while it is being used by
another thread.

The `CIContext` manages various internal state such as `MTLCommandQueue` and caches for compiled kernels
and intermediate buffers.  For this reason it is not recommended to create many `CIContext` instances.  As a rule,
it recommended that you create one `CIContext` instance for each view that renders [`CIImage`](/documentation/CoreImage/CIImage) or each background task.

## Topics

### Creating a Context Without Specifying a Destination

[`context`](/documentation/CoreImage/CIContext/context)

Creates a context without a specific rendering destination, using default options.

[`init()`](/documentation/CoreImage/CIContext/init())

Initializes a context without a specific rendering destination, using default options.

[`contextWithOptions:`](/documentation/CoreImage/CIContext/contextWithOptions:)

Initializes a context without a specific rendering destination, using the specified options.

### Creating a Context for CPU-Based Rendering

[`init(cgContext:options:)`](/documentation/CoreImage/CIContext/init(cgContext:options:)-6p78w)

Creates a Core Image context from a Quartz context, using the specified options.

### Creating a Context for GPU-Based Rendering

[`init(mtlDevice:)`](/documentation/CoreImage/CIContext/init(mtlDevice:)-swey)

Creates a Core Image context using the specified Metal device.

[`init(mtlDevice:options:)`](/documentation/CoreImage/CIContext/init(mtlDevice:options:)-26usb)

Creates a Core Image context using the specified Metal device and options.

[`init(mtlCommandQueue:)`](/documentation/CoreImage/CIContext/init(mtlCommandQueue:)-7dtqk)

[`init(mtlCommandQueue:options:)`](/documentation/CoreImage/CIContext/init(mtlCommandQueue:options:)-6i3me)

### Rendering Images

[`createCGImage(_:from:)`](/documentation/CoreImage/CIContext/createCGImage(_:from:))

Creates a Core Graphics image from a region of a Core Image image instance.

[`createCGImage(_:from:format:colorSpace:)`](/documentation/CoreImage/CIContext/createCGImage(_:from:format:colorSpace:))

Creates a Core Graphics image from a region of a Core Image image instance
with an option for controlling the pixel format and color space of the `CGImage`.

[`createCGImage(_:from:format:colorSpace:deferred:)`](/documentation/CoreImage/CIContext/createCGImage(_:from:format:colorSpace:deferred:))

Creates a Core Graphics image from a region of a Core Image image instance
with an option for controlling when the image is rendered.

[`render(_:toBitmap:rowBytes:bounds:format:colorSpace:)`](/documentation/CoreImage/CIContext/render(_:toBitmap:rowBytes:bounds:format:colorSpace:))

Renders to the given bitmap.

[`render(_:to:)`](/documentation/CoreImage/CIContext/render(_:to:))

Renders an image into a pixel buffer.

[`render(_:to:bounds:colorSpace:)`](/documentation/CoreImage/CIContext/render(_:to:bounds:colorSpace:)-2k8l2)

Renders a region of an image into a pixel buffer.

[`render(_:to:bounds:colorSpace:)`](/documentation/CoreImage/CIContext/render(_:to:bounds:colorSpace:)-54b9l)

Renders a region of an image into an IOSurface object.

[`render(_:to:commandBuffer:bounds:colorSpace:)`](/documentation/CoreImage/CIContext/render(_:to:commandBuffer:bounds:colorSpace:))

Renders a region of an image to a Metal texture.

### Drawing Images

[`draw(_:in:from:)`](/documentation/CoreImage/CIContext/draw(_:in:from:))

Renders a region of an image to a rectangle in the context destination.

### Determining the Allowed Extents for Images Used by a Context

[`inputImageMaximumSize()`](/documentation/CoreImage/CIContext/inputImageMaximumSize())

Returns the maximum size allowed for any image rendered into the context.

[`outputImageMaximumSize()`](/documentation/CoreImage/CIContext/outputImageMaximumSize())

Returns the maximum size allowed for any image created by the context.

### Managing Resources

[`clearCaches()`](/documentation/CoreImage/CIContext/clearCaches())

Frees any cached data, such as temporary images, associated with the context and runs the garbage collector.

[`reclaimResources()`](/documentation/CoreImage/CIContext/reclaimResources())

Runs the garbage collector to reclaim any resources that the context no longer requires.

[`offlineGPUCount()`](/documentation/CoreImage/CIContext/offlineGPUCount())

Returns the number of GPUs not currently driving a display.

[`workingColorSpace`](/documentation/CoreImage/CIContext/workingColorSpace)

The working color space of the Core Image context.

[`workingFormat`](/documentation/CoreImage/CIContext/workingFormat)

The working pixel format of the Core Image context.

### Rendering Images for Data or File Export

[`tiffRepresentation(of:format:colorSpace:options:)`](/documentation/CoreImage/CIContext/tiffRepresentation(of:format:colorSpace:options:))

Renders the image and exports the resulting image data in TIFF format.

[`jpegRepresentation(of:colorSpace:options:)`](/documentation/CoreImage/CIContext/jpegRepresentation(of:colorSpace:options:))

Renders the image and exports the resulting image data in JPEG format.

[`pngRepresentation(of:format:colorSpace:options:)`](/documentation/CoreImage/CIContext/pngRepresentation(of:format:colorSpace:options:))

Renders the image and exports the resulting image data in PNG format.

[`heifRepresentation(of:format:colorSpace:options:)`](/documentation/CoreImage/CIContext/heifRepresentation(of:format:colorSpace:options:))

Renders the image and exports the resulting image data in HEIF format.

[`heif10Representation(of:colorSpace:options:)`](/documentation/CoreImage/CIContext/heif10Representation(of:colorSpace:options:))

Renders the image and exports the resulting image data in HEIF10 format.

[`openEXRRepresentation(of:options:)`](/documentation/CoreImage/CIContext/openEXRRepresentation(of:options:))

Renders the image and exports the resulting image data in open EXR format.

[`writeTIFFRepresentation(of:to:format:colorSpace:options:)`](/documentation/CoreImage/CIContext/writeTIFFRepresentation(of:to:format:colorSpace:options:))

Renders the image and exports the resulting image data as a file in TIFF format.

[`writeJPEGRepresentation(of:to:colorSpace:options:)`](/documentation/CoreImage/CIContext/writeJPEGRepresentation(of:to:colorSpace:options:))

Renders the image and exports the resulting image data as a file in JPEG format.

[`writePNGRepresentation(of:to:format:colorSpace:options:)`](/documentation/CoreImage/CIContext/writePNGRepresentation(of:to:format:colorSpace:options:))

Renders the image and exports the resulting image data as a file in PNG format.

[`writeHEIFRepresentation(of:to:format:colorSpace:options:)`](/documentation/CoreImage/CIContext/writeHEIFRepresentation(of:to:format:colorSpace:options:))

Renders the image and exports the resulting image data as a file in HEIF format.

[`writeHEIF10Representation(of:to:colorSpace:options:)`](/documentation/CoreImage/CIContext/writeHEIF10Representation(of:to:colorSpace:options:))

Renders the image and exports the resulting image data as a file in HEIF10 format.

[`writeOpenEXRRepresentation(of:to:options:)`](/documentation/CoreImage/CIContext/writeOpenEXRRepresentation(of:to:options:))

Renders the image and exports the resulting image data as a file in open EXR format.

[`CIImageRepresentationOption`](/documentation/CoreImage/CIImageRepresentationOption)

### Creating Depth Blur Filters

[`depthBlurEffectFilter(for:disparityImage:portraitEffectsMatte:hairSemanticSegmentation:glassesMatte:gainMap:orientation:options:)`](/documentation/CoreImage/CIContext/depthBlurEffectFilter(for:disparityImage:portraitEffectsMatte:hairSemanticSegmentation:glassesMatte:gainMap:orientation:options:))

Create a [`CIFilter`](/documentation/CoreImage/CIFilter-swift.class) instance for the supplied image data that can be used to apply a depth blur effect created with the supplied auxiliary images.

[`depthBlurEffectFilter(for:disparityImage:portraitEffectsMatte:hairSemanticSegmentation:orientation:options:)`](/documentation/CoreImage/CIContext/depthBlurEffectFilter(for:disparityImage:portraitEffectsMatte:hairSemanticSegmentation:orientation:options:))

Create a [`CIFilter`](/documentation/CoreImage/CIFilter-swift.class) instance for the supplied image data that can be used to apply a depth blur effect created with the supplied auxiliary images.

[`depthBlurEffectFilter(for:disparityImage:portraitEffectsMatte:orientation:options:)`](/documentation/CoreImage/CIContext/depthBlurEffectFilter(for:disparityImage:portraitEffectsMatte:orientation:options:))

Create a [`CIFilter`](/documentation/CoreImage/CIFilter-swift.class) instance for the supplied image data that can be used to apply a depth blur effect created with the supplied auxiliary images.

[`depthBlurEffectFilter(forImageData:options:)`](/documentation/CoreImage/CIContext/depthBlurEffectFilter(forImageData:options:))

Create a [`CIFilter`](/documentation/CoreImage/CIFilter-swift.class) instance for the supplied image data that can be used to apply a depth blur effect.

[`depthBlurEffectFilter(forImageURL:options:)`](/documentation/CoreImage/CIContext/depthBlurEffectFilter(forImageURL:options:))

Create a [`CIFilter`](/documentation/CoreImage/CIFilter-swift.class) instance for the supplied image URL that can be used to apply a depth blur effect.

### Constants

Keys to be used in the `options` dictionary when creating a `CIContext` object.

[`CIContextOption`](/documentation/CoreImage/CIContextOption)

An enum string type that your code can use to select different options when creating a Core Image context.

### Customizing Render Destination

[`prepareRender(_:from:to:at:)`](/documentation/CoreImage/CIContext/prepareRender(_:from:to:at:))

An optional call to warm up a [`CIContext`](/documentation/CoreImage/CIContext) so that subsequent calls to render with the same arguments run more efficiently.

[`startTask(toClear:)`](/documentation/CoreImage/CIContext/startTask(toClear:))

Fills the entire destination with black or clear depending on its [`alphaMode`](/documentation/CoreImage/CIRenderDestination/alphaMode).

[`startTask(toRender:from:to:at:)`](/documentation/CoreImage/CIContext/startTask(toRender:from:to:at:))

Renders a portion of an image to a point in the destination.

[`startTask(toRender:to:)`](/documentation/CoreImage/CIContext/startTask(toRender:to:))

Renders an image to a destination so that point (0, 0) of the image maps to point (0, 0) of the destination.

### Deprecated

[`init(cglContext:pixelFormat:colorSpace:options:)`](/documentation/CoreImage/CIContext/init(cglContext:pixelFormat:colorSpace:options:)-6rp6d)

Creates a Core Image context from a CGL context, using the specified options, color space, and pixel format object.

[`contextWithCGLContext:pixelFormat:options:`](/documentation/CoreImage/CIContext/contextWithCGLContext:pixelFormat:options:)

Creates a Core Image context from a CGL context, using the specified options and pixel format object.

[`init(eaglContext:)`](/documentation/CoreImage/CIContext/init(eaglContext:)-8ajef)

Creates a Core Image context from an EAGL context.

[`init(eaglContext:options:)`](/documentation/CoreImage/CIContext/init(eaglContext:options:)-6uyqj)

Creates a Core Image context from an EAGL context using the specified options.

[`init(forOfflineGPUAtIndex:)`](/documentation/CoreImage/CIContext/init(forOfflineGPUAtIndex:))

Creates an OpenGL-based Core Image context using a GPU that is not currently driving a display.

[`init(forOfflineGPUAtIndex:colorSpace:options:sharedContext:)`](/documentation/CoreImage/CIContext/init(forOfflineGPUAtIndex:colorSpace:options:sharedContext:))

Creates an OpenGL-based Core Image context using a GPU that is not currently driving a display, with the specified options.

[`createCGLayer(with:info:)`](/documentation/CoreImage/CIContext/createCGLayer(with:info:))

Creates a CGLayer object from the provided parameters.

[`draw(_:at:from:)`](/documentation/CoreImage/CIContext/draw(_:at:from:))

Renders a region of an image to a point in the context destination.



---

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)