<!--
{
  "availability" : [
    "iOS: 16.0.0 -",
    "iPadOS: 16.0.0 -",
    "macCatalyst: 16.0.0 -",
    "macOS: 13.0.0 -",
    "tvOS: 16.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 9.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/ImageRenderer",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI13ImageRendererC"
  },
  "title" : "ImageRenderer"
}
-->

# ImageRenderer

An object that creates images from SwiftUI views.

```
final class ImageRenderer<Content> where Content : View
```

## Overview

Use `ImageRenderer` to export bitmap image data from a SwiftUI view. You
initialize the renderer with a view, then render images on demand,
either by calling the [`render(rasterizationScale:renderer:)`](/documentation/SwiftUI/ImageRenderer/render(rasterizationScale:renderer:)) method, or
by using the renderer’s properties to create a
<doc://com.apple.documentation/documentation/CoreGraphics/CGImage>,
<doc://com.apple.documentation/documentation/AppKit/NSImage>, or
<doc://com.apple.documentation/documentation/UIKit/UIImage>.

By drawing to a [`Canvas`](/documentation/SwiftUI/Canvas) and exporting with an `ImageRenderer`,
you can generate images from any progammatically-rendered content, like
paths, shapes, gradients, and more. You can also render standard SwiftUI
views like [`Text`](/documentation/SwiftUI/Text) views, or containers of multiple view types.

The following example uses a private `createAwardView(forUser:date:)` method
to create a game app’s view of a trophy symbol with a user name and date.
This view combines a [`Canvas`](/documentation/SwiftUI/Canvas) that applies a shadow filter with
two [`Text`](/documentation/SwiftUI/Text) views into a [`VStack`](/documentation/SwiftUI/VStack). A [`Button`](/documentation/SwiftUI/Button) allows the person to
save this view. The button’s action uses an `ImageRenderer` to rasterize a
`CGImage` and then calls a private `uploadAchievementImage(_:)` method to
encode and upload the image.

```
var body: some View {
    let trophyAndDate = createAwardView(forUser: playerName,
                                         date: achievementDate)
    VStack {
        trophyAndDate
        Button("Save Achievement") {
            let renderer = ImageRenderer(content: trophyAndDate)
            if let image = renderer.cgImage {
                uploadAchievementImage(image)
            }
        }
    }
}

private func createAwardView(forUser: String, date: Date) -> some View {
    VStack {
        Image(systemName: "trophy")
            .resizable()
            .frame(width: 200, height: 200)
            .frame(maxWidth: .infinity, maxHeight: .infinity)
            .shadow(color: .mint, radius: 5)
        Text(playerName)
            .font(.largeTitle)
        Text(achievementDate.formatted())
    }
    .multilineTextAlignment(.center)
    .frame(width: 200, height: 290)
}
```

![A large trophy symbol, drawn with a mint-colored shadow. Below this, a](images/com.apple.SwiftUI/ImageRenderer-1~dark@2x.png)

Because `ImageRenderer` conforms to
<doc://com.apple.documentation/documentation/Combine/ObservableObject>, you
can use it to produce a stream of images as its properties change. Subscribe
to the renderer’s [`objectWillChange`](/documentation/SwiftUI/ImageRenderer/objectWillChange) publisher, then use the
renderer to rasterize a new image each time the subscriber receives an
update.

> Important: `ImageRenderer` output only includes views that SwiftUI renders,
> such as text, images, shapes, and composite views of these types. It
> does not render views provided by native platform frameworks (AppKit and
> UIKit) such as web views, media players, and some controls. For these views,
> `ImageRenderer` displays a placeholder image, similar to the behavior of
> ``doc://com.apple.SwiftUI/documentation/SwiftUI/View/drawingGroup(opaque:colorMode:)``.

### Rendering to a PDF context

The [`render(rasterizationScale:renderer:)`](/documentation/SwiftUI/ImageRenderer/render(rasterizationScale:renderer:)) method renders the specified
view to any
<doc://com.apple.documentation/documentation/CoreGraphics/CGContext>. That
means you aren’t limited to creating a rasterized `CGImage`. For
example, you can generate PDF data by rendering to a PDF context. The
resulting PDF maintains resolution-independence for supported members of the
view hierarchy, such as text, symbol images, lines, shapes, and fills.

The following example uses the `createAwardView(forUser:date:)` method from
the previous example, and exports its contents as an 800-by-600 point PDF to
the file URL `renderURL`. It uses the `size` parameter sent to the
rendering closure to center the `trophyAndDate` view vertically and
horizontally on the page.

```
var body: some View {
    let trophyAndDate = createAwardView(forUser: playerName,
                                        date: achievementDate)
    VStack {
        trophyAndDate
        Button("Save Achievement") {
            let renderer = ImageRenderer(content: trophyAndDate)
            renderer.render { size, renderer in
                var mediaBox = CGRect(origin: .zero,
                                      size: CGSize(width: 800, height: 600))
                guard let consumer = CGDataConsumer(url: renderURL as CFURL),
                      let pdfContext =  CGContext(consumer: consumer,
                                                  mediaBox: &mediaBox, nil)
                else {
                    return
                }
                pdfContext.beginPDFPage(nil)
                pdfContext.translateBy(x: mediaBox.size.width / 2 - size.width / 2,
                                       y: mediaBox.size.height / 2 - size.height / 2)
                renderer(pdfContext)
                pdfContext.endPDFPage()
                pdfContext.closePDF()
            }
        }
    }
}
```

### Creating an image from drawing instructions

`ImageRenderer` makes it possible to create a custom image by drawing into a
[`Canvas`](/documentation/SwiftUI/Canvas), rendering a `CGImage` from it, and using that to initialize an
[`Image`](/documentation/SwiftUI/Image). To simplify this process, use the `Image`
initializer [`init(size:label:opaque:colorMode:renderer:)`](/documentation/SwiftUI/Image/init(size:label:opaque:colorMode:renderer:)), which
takes a closure whose argument is a [`GraphicsContext`](/documentation/SwiftUI/GraphicsContext) that you can
directly draw into.

## Topics

### Creating an image renderer

[`init(content:)`](/documentation/SwiftUI/ImageRenderer/init(content:))

Creates a renderer object with a source content view.

### Providing the source view

[`content`](/documentation/SwiftUI/ImageRenderer/content)

The root view rendered by this image renderer.

### Accessing renderer properties

[`proposedSize`](/documentation/SwiftUI/ImageRenderer/proposedSize)

The size proposed to the root view.

[`scale`](/documentation/SwiftUI/ImageRenderer/scale)

The scale at which to render the image.

[`isOpaque`](/documentation/SwiftUI/ImageRenderer/isOpaque)

A Boolean value that indicates whether the alpha channel of the image is
fully opaque.

[`colorMode`](/documentation/SwiftUI/ImageRenderer/colorMode)

The working color space and storage format of the image.

[`allowedDynamicRange`](/documentation/SwiftUI/ImageRenderer/allowedDynamicRange)

The allowed dynamic range of the image, or nil to mark that the
dynamic range of the image should be unrestricted. This
property defaults to `sdr`, i.e. HDR content will be tone
mapped to SDR.

### Rendering images

[`render(rasterizationScale:renderer:)`](/documentation/SwiftUI/ImageRenderer/render(rasterizationScale:renderer:))

Draws the renderer’s current contents to an arbitrary Core Graphics
context.

[`cgImage`](/documentation/SwiftUI/ImageRenderer/cgImage)

The current contents of the view, rasterized as a Core Graphics image.

[`nsImage`](/documentation/SwiftUI/ImageRenderer/nsImage)

The current contents of the view, rasterized as an AppKit image.

[`uiImage`](/documentation/SwiftUI/ImageRenderer/uiImage)

The current contents of the view, rasterized as a UIKit image.

### Producing a stream of images

[`objectWillChange`](/documentation/SwiftUI/ImageRenderer/objectWillChange)

A publisher that informs subscribers of changes to the image.

[`isObservationEnabled`](/documentation/SwiftUI/ImageRenderer/isObservationEnabled)

If observers of this observed object should be notified when the
produced image changes.



---

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)
