One Logo Pool Ball
SwiftUICore · IOS API REFERENCE

ImageRenderer

class · iOS 16.0+ · SDK 27.1

final class ImageRenderer<Content> where Content : View

An object that creates images from SwiftUI views. 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:)`` 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`` 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`` 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`` that applies a shadow filter with two ``Text`` views into a ``VStack``. A ``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 user name and the date and time. At the bottom, a button with the title Save Achievement allows people to save and upload an image of this view.](ImageRenderer-1) 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 ``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 rasterizes directly using its own drawing primitives, such as text, images, shapes, and composite views of these types. It does not include views whose contents are composited by Core Animation layers, such as more complex controls and containers, web views, media players, and most types of UIKit and AppKit views. In those cases, `ImageRenderer` displays a placeholder image, similar to the behavior of ``View/drawingGroup(opaque:colorMode:)``. Whether a particular view is rendered using SwiftUI's own drawing primitives or composited by Core Animation may change in future releases. However, any view that is currently supported is guaranteed to remain supported. ### Rendering to a PDF context The ``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``, rendering a `CGImage` from it, and using that to initialize an ``Image``. To simplify this process, use the `Image` initializer ``Image/init(size:label:opaque:colorMode:renderer:)``, which takes a closure whose argument is a ``GraphicsContext`` that you can directly draw into.

Members (0)

This type has no directly callable members in this snapshot. See the framework’s skipped declarations for unsupported APIs.

Skipped declarations

1 declarations were excluded from these bindings. Each reason comes from the generator or the iOS SDK compiler.

ImageRenderer.render(rasterizationScale:renderer:)

The iOS SDK compiler rejected this binding: converting non-escaping value to 'T' may allow it to escape

All SwiftUICore types →