Update README.md
Demo repository to showcase how to programmatically generate images from a command-line tool with SwiftUI.
$ mkdir mytool && cd mytool $ swift package init --type executable $ swift run Hello, world!
Xcode does not support live previews on Swift packages yet (FB8979344). To allow for realtime previews follow these steps:
$ swift package generate-xcodeproj $ open mytool.xcodeproj
Create a view with a red label over white background
import SwiftUI struct MySwiftUIView : View { var body: some View { ZStack { Color.blue.edgesIgnoringSafeArea(.all) Text("Hello, world!") .foregroundColor(.white) .font(.largeTitle) } } } #if DEBUG struct MySwiftUIView_Previews : PreviewProvider { static var previews: some View { MySwiftUIView() } } #endif
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
NSHostingView
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"))
$ swift run && open test.png
©Copyright 2023 CCF 开源发展委员会 Powered by Trustie& IntelliDE 京ICP备13000930号
ConsoleUI
Demo repository to showcase how to programmatically generate images from a command-line tool with SwiftUI.
The Basics: Hello, world!
Create a new command-line executable
Generate Xcode project (optional)
Xcode does not support live previews on Swift packages yet (FB8979344). To allow for realtime previews follow these steps:
Add SwiftUI view
Create a view with a red label over white background
Rasterize view and save to file
We will use this method to rasterize views to images:
The following code instantiates the SwiftUI view, wrapped inside a
NSHostingView
which is then rasterized and saved to diskRun the command and open in Preview