2

I’m working in a App using Swift that includes PencilKit with a “canvasview” to take notes. Everything works fine, but I can’t find the way to “convert” de value returned by canvasView.drawing to String for uploaded it and storage into a database.

I can get a image and even a "base64EncodedString” but I need a way to send the “drawing” to a server and then load from it, and show in the canvasView.

I have tested to convert it into a json, but I can’t “deserialize” and convert it into a “drawing” again, when is loaded from the server hehe.

Thanks in advance

byaruhaf
  • 4,128
  • 2
  • 32
  • 50
oscarabilleira
  • 121
  • 1
  • 12

1 Answers1

5

You only have one option if you want to restore it to a PKDrawing object- use drawing.dataRepresentation() to get a Data object you can store or write out etc. For example, I use this to save to a SQL database:

self.pageEntity?.setValue(self.canvasView.drawing.dataRepresentation(), forKey: "markup")

You can't interpret the results of the data object, but you can re-import it into a PKDrawing like this:

do {
    try d = PKDrawing.init(data: markup)
        canvasView.drawing = d
    } catch {
        print("Error loading drawing object")
    }
}
David Peat
  • 244
  • 2
  • 5