I'm using tutorial from here https://www.hackingwithswift.com/quick-start/swiftui/how-to-convert-a-swiftui-view-to-an-image
extension View {
func snapshot() -> UIImage {
let controller = UIHostingController(rootView: self)
let view = controller.view
let targetSize = controller.view.intrinsicContentSize
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)
}
}
}
And I want to apply this code to
Text("Hello world!")
.swipeActions(edge: .leading) {
Button {
total += 1
} label: {
Label("Add", systemImage: "plus.circle")
}
.tint(.indigo)
}
.swipeActions(edge: .trailing) {
Button {
total -= 1
} label: {
Label {
Text("Hello")
} icon: {
Image(uiImage:
Text("World")
.foregroundColor(.red)
.snapshot() // Here is usage
)
}
}
}
But in result I get in this particular case empty gray button (and in my real code runtime error about changing in time of redrawing)
What's wrong?