SwiftUI Views to Images
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
January 13, 2021 3:06 AM
FYI: A UIHostingView sets the background color to the system bg. Transparent captures require setting backgroundColor to clear.