Monday, January 11, 2021 [Tweets] [Favorites]

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

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

Stay up-to-date by subscribing to the Comments RSS Feed for this post.

Leave a Comment