Using PencilKit in Swift. What is the best way to save PKDrawing into a remote 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

Accepted Reply

As far as I checked the doc, `PKDrawing` has an initilizer taking `Data`.

init(data: Data)


Does that not work?


If you prefer sending and receiving the data as Base-64, you can write an extension like this:

extension PKDrawing {
    func base64EncodedString() -> String {
        return dataRepresentation().base64EncodedString()
    }
    
    enum DecodingError: Error {
        case decodingError
    }
    
    init(base64Encoded base64: String) throws {
        guard let data = Data(base64Encoded: base64) else {
            throw DecodingError.decodingError
        }
        try self.init(data: data)
    }
}

Replies

As far as I checked the doc, `PKDrawing` has an initilizer taking `Data`.

init(data: Data)


Does that not work?


If you prefer sending and receiving the data as Base-64, you can write an extension like this:

extension PKDrawing {
    func base64EncodedString() -> String {
        return dataRepresentation().base64EncodedString()
    }
    
    enum DecodingError: Error {
        case decodingError
    }
    
    init(base64Encoded base64: String) throws {
        guard let data = Data(base64Encoded: base64) else {
            throw DecodingError.decodingError
        }
        try self.init(data: data)
    }
}

Thank's a lot... I was very aesy 😁.


It was just what I needed

I realise this was some time ago now but I am working on something similar and would be very interested to see any code you have relating to saving PK drawings to a server. I would be really interested to know how you uploaded and stored the base64encoded String in the database, was it in a blob or text field for example? If you have any hints on the best way to tackle this issue I would be keen to hear about them.
Thanks!

Maybe this can help

func dataRepresentation() -> Data // << to store data in file
init(data: Data) throws // << to restore from data read from file

Hi, I try to load an png image in my PKDrawing, but it seems that init(data:) doesn't work like expected. Here is my code

var base64 = sign!.replacingOccurrences(of: "data:image/png;base64,", with: "")
base64 = base64.replacingOccurrences(of: "\r", with: "")
base64 = base64.replacingOccurrences(of: "\n", with: "")
guard let dataDecoded = Data(base64Encoded: base64, options: .ignoreUnknownCharacters) else {
    return
}

do {
    self.canvasView.drawing = try PKDrawing.init(data: dataDecoded)
} catch {
    print("Error loading drawing object")
}

No error catched but nothing in my canvas. I also tried append(_ :PKDrawing) but it's the same.

Any ideas? What is my mistake? I work in SwiftUI.