Im trying to snapshot a SwiftUI View, but when I update that view (the text for example) and I snapshot the View, the image I get back is from the default view (previous render of the View, not the updated one)
On this code example there is a TextView to be snapshotted, when tapped it changes/updates, then pressing the button should save the new updated View with the TextView changed, but instead it saves the old version.
Extension on View to snapshot View:
extension View {
func snapshot() -> UIImage {
let controller = UIHostingController(rootView: self)
let view = controller.view
let targetSize = CGSize(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
view?.bounds = CGRect(origin: .zero, size: targetSize)
view?.backgroundColor = .clear
let renderer = UIGraphicsImageRenderer(size: targetSize)
return renderer.image { _ in
view?.drawHierarchy(in: controller.view.bounds, afterScreenUpdates: true)
}
}
}
ContentView with button to snapshot View:
struct ContentView: View {
var view: some View = ScreenShot()
var body: some View {
VStack {
view
Button("Save Image") {
let image = view.snapshot()
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
}
}
}
}
View to be snapshotted and updated:
struct ScreenShot: View {
@State var updateView = false
var body: some View {
Text("View to be snapshotted \(updateView ? "(Update)" : "")")
.tapGesture {
updateView.toggle()
}
}
}