Rasterizing SwiftUI views from the Command-Line

Generating images programmatically can be used for many purposes. From generating simple memes (by overlaying some text over a given image asset), to composing image collages, applying filters, image transformations, charts, infographics, etc.

SwiftUI is a powerful DSL designed to make it very easy to generate complex views with code, while removing a great deal of boilerplate.

Good news is SwiftUI can be used from the command-line¹ with Swift Package Manager.

Here is how-to.

The basics: Hello World!

Let’s start by creating a new command-line executable

$ mkdir mytool && cd mytool
$ swift package init --type executable
$ swift run
Hello, world!

Generate the Xcode project and open in Xcode 11

$ swift package generate-xcodeproj
$ open mytool.xcodeproj

Add a SwiftUI view

Create a view with a red label and white background

import SwiftUI

struct MySwiftUIView : View {
var body: some View {
ZStack {
Text("Hello, world!").color(.red).font(.largeTitle)

struct MySwiftUIView_Previews : PreviewProvider {
static var previews: some View {

Rasterize view and save to file

We will use this method to rasterize views to images:

func rasterize(view: NSView, 
format: NSBitmapImageRep.FileType) -> Data? {
guard let bitmapRepresentation = view
.bitmapImageRepForCachingDisplay(in: view.bounds) else {
return nil
bitmapRepresentation.size = view.bounds.size
view.cacheDisplay(in: view.bounds, to: bitmapRepresentation)
return bitmapRepresentation
.representation(using: format, properties: [:])

The following code instantiates the SwiftUI view, wrapped inside a NSHostingView which is then rasterized and saved to disk

let wrapper = NSHostingView(rootView: MySwiftUIView())
wrapper.frame = CGRect(x: 0, y: 0, width: 800, height: 450)
let png = rasterize(view: wrapper, format: .png)
try png?.write(to: URL(fileURLWithPath: "test.png"))
Full contents of main.swift

Run the command and open in Preview

$ swift run && open test.png
Preview application displaying the generated image

The source code for this article is available on GitHub: https://github.com/eneko/ConsoleUI

[1] Using SwiftUI on the command-line requires macOS 10.15 (Catalina)



Senior Engineer at MINDBODY. I specialize in Swift applications (mobile and server-side).

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Eneko Alonso

Senior Engineer at MINDBODY. I specialize in Swift applications (mobile and server-side).