Monday, January 11, 2021

SwiftUI Views to Images

Alejandro Martinez:

Sadly, SwiftUI doesn’t provide a native way of generating images from its views. We need to resort to tricks used in its ancestor frameworks.

[…]

First, we need to create a NSHostingView with the SwiftUI view that you want to create the image from.

With an NSView in hand, the rest of the process is no different that what you would do to rasterize a native AppKit view.

1 Comment RSS · Twitter

FYI: A UIHostingView sets the background color to the system bg. Transparent captures require setting backgroundColor to clear.

Leave a Comment