<!--
{
  "availability" : [
    "iOS: 10.0.0 -",
    "iPadOS: 10.0.0 -",
    "macCatalyst: 13.1.0 -",
    "tvOS: 10.0.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "UIKit",
  "identifier" : "/documentation/UIKit/UIGraphicsImageRenderer",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "UIKit"
    ],
    "preciseIdentifier" : "c:objc(cs)UIGraphicsImageRenderer"
  },
  "title" : "UIGraphicsImageRenderer"
}
-->

# UIGraphicsImageRenderer

A graphics renderer for creating Core Graphics-backed images.

```
class UIGraphicsImageRenderer
```

## Overview

You can use image renderers to accomplish drawing tasks, without having to handle configuration such as color depth and image scale, or manage Core Graphics contexts. You initialize an image renderer with parameters such as image output dimensions and format. You then use one or more of the drawing functions to render images that share these properties.

To render an image:

1. Optionally, create a [`UIGraphicsImageRendererFormat`](/documentation/UIKit/UIGraphicsImageRendererFormat) object to specify nondefault parameters the renderer should use to create its context.
2. Instantiate a [`UIGraphicsImageRenderer`](/documentation/UIKit/UIGraphicsImageRenderer) object, providing the dimensions of the output image and a format object. The renderer uses default values for the current device if you don’t provide format object, as demonstrated in [`Creating a graphics image renderer`](/documentation/UIKit/UIGraphicsImageRenderer#Creating-a-graphics-image-renderer).
3. Choose one of the rendering methods depending on the output you desire: [`image(actions:)`](/documentation/UIKit/UIGraphicsImageRenderer/image(actions:)) returns a [`UIImage`](/documentation/UIKit/UIImage) object; [`jpegData(withCompressionQuality:actions:)`](/documentation/UIKit/UIGraphicsImageRenderer/jpegData(withCompressionQuality:actions:)) returns a JPEG-encoded <doc://com.apple.documentation/documentation/Foundation/Data> object; and [`pngData(actions:)`](/documentation/UIKit/UIGraphicsImageRenderer/pngData(actions:)) returns a PNG-encoded <doc://com.apple.documentation/documentation/Foundation/Data> object.
4. Execute your chosen method, providing Core Graphics drawing instructions as the closure argument, as shown in [`Creating an image with an image renderer`](/documentation/UIKit/UIGraphicsImageRenderer#Creating-an-image-with-an-image-renderer). [`Using blend mode`](/documentation/UIKit/UIGraphicsImageRenderer#Using-blend-mode) demonstrates some of the more advanced rendering features you can use in your drawing instructions.
5. Optionally, you can use Core Graphics drawing code within the drawing instructions you provide to the rendering method, as shown in [`Using Core Graphics rendering functions`](/documentation/UIKit/UIGraphicsImageRenderer#Using-Core-Graphics-rendering-functions).

After initializing an image renderer, you can use it to draw multiple images with the same configuration. An image renderer keeps a cache of Core Graphics contexts, so reusing the same renderer can be more efficient than creating new renderers.

### Creating a graphics image renderer

Create an image renderer, providing the size of the output image:

```swift
let renderer = UIGraphicsImageRenderer(size: CGSize(width: 200, height: 200))
```

You can instead use one of the other [`UIGraphicsImageRenderer`](/documentation/UIKit/UIGraphicsImageRenderer) initializers to specify a renderer format ([`UIGraphicsImageRendererFormat`](/documentation/UIKit/UIGraphicsImageRendererFormat)) in addition to the size. This allows you to configure the underlying Core Graphics context for wide color and retina images.

If you don’t provide a format, the renderer uses the [`default()`](/documentation/UIKit/UIGraphicsRendererFormat/default()) format, which creates a context best suited for the current device.

### Creating an image with an image renderer

Use the [`image(actions:)`](/documentation/UIKit/UIGraphicsImageRenderer/image(actions:)) method to create an image ([`UIImage`](/documentation/UIKit/UIImage) object) with an image renderer. This method takes a closure that represents the drawing actions. Within this closure, the renderer creates a Core Graphics context using the parameters provided during renderer initialization, and sets this Core Graphics context to be the current context.

```swift
let image = renderer.image { (context) in
  UIColor.darkGray.setStroke()
  context.stroke(renderer.format.bounds)
  UIColor(colorLiteralRed: 158/255, green: 215/255, blue: 245/255, alpha: 1).setFill()
  context.fill(CGRect(x: 1, y: 1, width: 140, height: 140))
}
```

The drawing actions closure takes a single argument of type [`UIGraphicsImageRendererContext`](/documentation/UIKit/UIGraphicsImageRendererContext). This provides access to some high-level drawing functions, such as [`fill(_:)`](/documentation/UIKit/UIGraphicsRendererContext/fill(_:)), through the [`UIGraphicsRendererContext`](/documentation/UIKit/UIGraphicsRendererContext) superclass.

The above code creates the following image:

![Image showing a blue square in the top left of a larger white squares](images/com.apple.uikit/media-2874999@2x.png)

In addition to the [`image(actions:)`](/documentation/UIKit/UIGraphicsImageRenderer/image(actions:)) method that creates an [`UIImage`](/documentation/UIKit/UIImage) object, [`UIGraphicsImageRenderer`](/documentation/UIKit/UIGraphicsImageRenderer) also has [`jpegData(withCompressionQuality:actions:)`](/documentation/UIKit/UIGraphicsImageRenderer/jpegData(withCompressionQuality:actions:)) and [`pngData(actions:)`](/documentation/UIKit/UIGraphicsImageRenderer/pngData(actions:)) methods that create <doc://com.apple.documentation/documentation/Foundation/Data> objects containing the image encoded as a JPEG or a PNG respectively. All three methods take the same approach as detailed here, accepting a block that represents the drawing actions.

### Using blend mode

The utility methods on [`UIGraphicsImageRendererContext`](/documentation/UIKit/UIGraphicsImageRendererContext) also offer a variant that accepts a <doc://com.apple.documentation/documentation/CoreGraphics/CGBlendMode> value. This value determines how to combine the pixel values when painting.

```swift
let image = renderer.image { (context) in
  UIColor.darkGray.setStroke()
  context.stroke(renderer.format.bounds)
  UIColor(colorLiteralRed: 158/255, green: 215/255, blue: 245/255, alpha: 1).setFill()
  context.fill(CGRect(x: 1, y: 1, width: 140, height: 140))
  UIColor(colorLiteralRed: 145/255, green: 211/255, blue: 205/255, alpha: 1).setFill()
  context.fill(CGRect(x: 60, y: 60, width: 140, height: 140), blendMode: .multiply)
}
```

This code draws a second square, using a blend mode of multiply. The following image shows the result.

![Image showing two overlapping squares, one blue, the other turquoise, in the top-left and bottom-right of a white background square respectively.](images/com.apple.uikit/media-2875000@2x.png)

### Using Core Graphics rendering functions

The [`UIGraphicsImageRendererContext`](/documentation/UIKit/UIGraphicsImageRendererContext) available in the image closure has a [`cgContext`](/documentation/UIKit/UIGraphicsRendererContext/cgContext) property, which allows you to use Core Graphics rendering functions directly. For example, the following code demonstrates how to add a circle to the image:

```swift
let image = renderer.image { (context) in
  UIColor.darkGray.setStroke()
  context.stroke(renderer.format.bounds)
  UIColor(colorLiteralRed: 158/255, green: 215/255, blue: 245/255, alpha: 1).setFill()
  context.fill(CGRect(x: 1, y: 1, width: 140, height: 140))
  UIColor(colorLiteralRed: 145/255, green: 211/255, blue: 205/255, alpha: 1).setFill()
  context.fill(CGRect(x: 60, y: 60, width: 140, height: 140), blendMode: .multiply)
  
  UIColor(colorLiteralRed: 203/255, green: 222/255, blue: 116/255, alpha: 0.6).setFill()
  context.cgContext.fillEllipse(in: CGRect(x: 60, y: 60, width: 140, height: 140))
}
```

This code uses the <doc://com.apple.documentation/documentation/CoreGraphics/CGContext/fillEllipse(in:)> method on <doc://com.apple.documentation/documentation/CoreGraphics/CGContext> to draw a green circle on the blue and turquoise squares image; the following image shows the result.

![An image showing two overlapping squares and an overlaid green circle.](images/com.apple.uikit/media-2875001@2x.png)

## Topics

### Initializing an image renderer

[`-  initWithBounds:format:`](/documentation/UIKit/UIGraphicsImageRenderer/init(bounds:format:))

Creates an image renderer with the specified bounds and format.

[`-  initWithSize:`](/documentation/UIKit/UIGraphicsImageRenderer/init(size:))

Creates an image renderer for drawing images of the specified size.

[`-  initWithSize:format:`](/documentation/UIKit/UIGraphicsImageRenderer/init(size:format:))

Creates an image renderer with the specified size and format.

### Creating images

[`-  imageWithActions:`](/documentation/UIKit/UIGraphicsImageRenderer/image(actions:))

Creates an image from a set of drawing instructions.

[`-  JPEGDataWithCompressionQuality:actions:`](/documentation/UIKit/UIGraphicsImageRenderer/jpegData(withCompressionQuality:actions:))

Creates a JPEG-encoded image from a set of drawing instructions.

[`-  PNGDataWithActions:`](/documentation/UIKit/UIGraphicsImageRenderer/pngData(actions:))

Creates a PNG-encoded image from a set of drawing instructions.

[`DrawingActions`](/documentation/UIKit/UIGraphicsImageRenderer/DrawingActions)

A closure for drawing an image.

## Relationships

### Inherits From

[`UIGraphicsRenderer`](/documentation/UIKit/UIGraphicsRenderer)

### Conforms To

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

[`Hashable`](/documentation/Swift/Hashable)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`CVarArg`](/documentation/Swift/CVarArg)

[`Sendable`](/documentation/Swift/Sendable)

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

[`Equatable`](/documentation/Swift/Equatable)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

---

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)