Type Method

contextWithCGLContext:pixelFormat:colorSpace:options:

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

Declaration

+ (CIContext *)contextWithCGLContext:(CGLContextObj)cglctx pixelFormat:(CGLPixelFormatObj)pixelFormat colorSpace:(CGColorSpaceRef)colorSpace options:(NSDictionary<CIContextOption, id> *)options;

Parameters

ctx

A CGL context obtained by calling the CGL function CGLCreateContext.

pf

A CGL pixel format object either obtained from the system or created by calling a CGL function such as CGLChoosePixelFormat. This parameter must be the same pixel format object used to create the CGL context. The pixel format object must be valid for the lifetime of the Core Image context. Don’t release the pixel format object until after you release the Core Image context.

space

A color space object encapsulating color space information that is used to specify how color values are interpreted.

options

A dictionary that contains options for creating a CIContext object. You can pass any of the keys defined in Context Options along with the appropriate value.

Discussion

After calling this method, Core Image draws content into the surface (drawable object) attached to the CGL context. A CGL context is a macOS OpenGL context. For more information, see OpenGL Programming Guide for Mac.

When you create a CIContext object using a CGL context, all OpenGL states set for the CGL context affect rendering to that context. That means that coordinate and viewport transformations set on the CGL context, as well as the vertex color, affect drawing to that context.

For best results, follow these guidelines when you use Core Image to render into an OpenGL context:

  • Ensure that a single unit in the coordinate space of the OpenGL context represents a single pixel in the output device.

  • The Core Image coordinate space has the origin in the bottom-left corner of the screen. You should configure the OpenGL context in the same way.

  • The OpenGL context blending state is respected by Core Image. If the image you want to render contains translucent pixels, it’s best to enable blending using a blend function with the parameters GL_ONE, GL_ONE_MINUS_SRC_ALPHA, as shown in the following code example.

Core Image manages its own internal OpenGL context that shares resources with the OpenGL context you specify. To enable resource sharing, use the following code:

const NSOpenGLPixelFormatAttribute attr[] = {
        NSOpenGLPFAAccelerated,
        NSOpenGLPFANoRecovery,
        NSOpenGLPFAColorSize, 32,
        0
    };
NSOpenGLPixelFormat *pf = [[NSOpenGLPixelFormat alloc] initWithAttributes:(void *)&attr];
CIContext *myCIContext = [CIContext contextWithCGLContext: CGLGetCurrentContext()
                                pixelFormat: [pf CGLPixelFormatObj]
                                colorSpace: CGColorSpaceCreateDeviceRGB()
                                options: nil];

See Also

Creating a Context for GPU-Based Rendering with OpenGL

+ contextWithCGLContext:pixelFormat:options:

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

Deprecated
+ contextWithEAGLContext:

Creates a Core Image context from an EAGL context.

Deprecated
+ contextWithEAGLContext:options:

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

Deprecated
+ contextForOfflineGPUAtIndex:

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

Deprecated
+ contextForOfflineGPUAtIndex:colorSpace:options:sharedContext:

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

Deprecated