Contents

render(in:)

Renders the layer and its sublayers into the specified context.

Declaration

func render(in ctx: CGContext)

Parameters

  • ctx:

    The graphics context to use to render the layer.

Discussion

This method renders directly from the layer tree, ignoring any animations added to the render tree. Renders in the coordinate space of the layer.

The following code shows how you can use render(in:) to create a UIImage from a CAShapeLayer with a path that describes a circle. After creating the layer, the code creates a CGContext into which the circle is rendered. After rendering, UIGraphicsGetImageFromCurrentImageContext() generates the image.

let diameter: CGFloat = 100
let rect = CGRect(origin: CGPoint.zero,
                  size: CGSize(width: diameter, height: diameter))
    
let shapeLayer = CAShapeLayer()
shapeLayer.fillColor = UIColor.white.cgColor
shapeLayer.lineWidth = 10
shapeLayer.path = CGPath(ellipseIn: rect,
                         transform: nil)
        
let renderer = UIGraphicsImageRenderer(size: rect.size)
     
let image = renderer.image {
    context in

    return shapeLayer.render(in: context.cgContext)
}

See Also

Configuring the layer’s rendering behavior