I want to save my canvas to album, but my subview is not visible when I go back to album. I only have my canvas with my drawings.
What should I change ?
struct Home : View {
@State var canvas = PKCanvasView()
@State var showingAlert = false
var body: some View{
NavigationView{
MyCanvas(canvasView: canvas)
.navigationBarTitleDisplayMode(.inline)
.navigationBarItems(
leading:
HStack{
Button(action: { SaveImage() }, label: { Image(systemName: "square.and.arrow.down.fill").padding() })
})
}
}
My function to save image to album :
func SaveImage(){
// getting image from canvas....
let image = canvas.drawing.image(from: canvas.drawing.bounds, scale: 1)
// saving to albums....
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
showingAlert = true
}
}
My Canvas, where I add my background image :
struct MyCanvas: UIViewRepresentable {
var canvasView: PKCanvasView
let picker = PKToolPicker.init()
func makeUIView(context: Context) -> PKCanvasView {
self.canvasView.tool = PKInkingTool(.pen, color: .black, width: 15)
self.canvasView.isOpaque = false
self.canvasView.backgroundColor = UIColor.clear
self.canvasView.becomeFirstResponder()
let imageView = UIImageView(image: UIImage(named: "badmintoncourt"))
let subView = self.canvasView.subviews[0]
subView.addSubview(imageView)
subView.sendSubviewToBack(imageView)
return canvasView
}
func updateUIView(_ uiView: PKCanvasView, context: Context) {
picker.addObserver(canvasView)
picker.setVisible(true, forFirstResponder: uiView)
DispatchQueue.main.async {
uiView.becomeFirstResponder()
}
}
}
This is actually not answering the question, but I save my drawing like this to handle dark mode as well:
with the thumbnail function: