Convert SwiftUI views to images in a variety of formats.
ViewToImage lets you turn SwiftUI views into images in common formats like HEIC, JPEG, and PNG.
You can add ViewToImage as a dependency to your project using Swift Package Manager by adding it to the dependencies value of your Package.swift.
dependencies: [
.package(url: "https://github.com/kevinhermawan/swift-view-to-image.git", .upToNextMajor(from: "1.0.0"))
],
targets: [
.target(
/// ...
dependencies: [.product(name: "ViewToImage", package: "swift-view-to-image")])
]Alternatively, in Xcode:
- Open your project in Xcode.
- Click on
File->Swift Packages->Add Package Dependency... - Enter the repository URL:
git@github.com:kevinhermawan/swift-view-to-image.git - Choose the version you want to add. You probably want to add the latest version.
- Click
Add Package.
You can find the documentation here: https://kevinhermawan.github.io/swift-view-to-image/documentation/viewtoimage
import SwiftUI
import ViewToImage
struct GreetingView: View {
var body: some View {
Text("Hello, World!")
.padding()
.background(Color.purple)
}
}
if let imageData = ViewToImage.png(from: GreetingView(), size: CGSize(width: 300, height: 100)) {
print("Image successfully created!")
}If you find ViewToImage helpful and would like to support its development, consider making a donation. Your contribution helps maintain the project and develop new features.
Your support is greatly appreciated! ❤️
Contributions are welcome! Please open an issue or submit a pull request if you have any suggestions or improvements.
This repository is available under the Apache License 2.0.