oscarabilleira
oscarabilleira

Reputation: 181

Using PencilKit in Swift. What is the best way to save PKDrawing into a database? How to serialize it?

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

Upvotes: 3

Views: 2161

Answers (1)

David Peat
David Peat

Reputation: 254

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")
    }
}

Upvotes: 6

Related Questions