CGLayer

An offscreen context for reusing content drawn with Core Graphics.

Overview

Layer objects are useful for offscreen drawing and can be used in much the same way that a bitmap context can be used. In fact, a CGLayer object is a much better representation than a bitmap context.

Using CGLayer objects can improve performance, particularly when you need to capture a piece of drawing that you stamp repeatedly (using the same scale factor and orientation). Core Graphics can cache CGLayer objects to the video card, making drawing a CGLayer to a destination much faster than rendering the equivalent image constructed from a bitmap context.

A CGLayer object is created relative to a graphics context. Although layer uses this graphics context as a reference for initialization, you are not restricted to drawing the layer to this graphics context. You can draw the layer to other graphics contexts, although any limitations of the original context are imposed. For example, if you create a CGLayer object using a bitmap context, the layer is rendered as a bitmap when drawn to any other graphics context.

You can use a CGLayer when you want to apply a shadow to a group of objects (such as a group of circles) rather than to individual objects.

Use these layers in your code whenever you can, especially when:

  • You need to reuse a filled or stroked shape.

  • You are building a scene and at least some of it can be reused. Put the reusable drawing in its own layer.

Any Core Graphics object that you draw repeatedly—including CGPath, CGShading, and CGPDFPage objects—benefit from improved performance if you draw it to a CGLayer object.

Symbols

Creating Layer Objects

init?(CGContext, size: CGSize, auxiliaryInfo: CFDictionary?)

Creates a layer object that is associated with a graphics context.

Getting the CFType ID for a Layer

class var typeID: CFTypeID

Returns the unique type identifier used for CGLayer objects.

Getting Layer Information

var size: CGSize

Returns the width and height of a layer object.

var context: CGContext?

Returns the graphics context associated with a layer object.

Data Types

CGLayer

An offscreen context for reusing content drawn with Core Graphics.