ATSketchKit icon indicating copy to clipboard operation
ATSketchKit copied to clipboard

Save draft?

Open steelzeh opened this issue 8 years ago • 3 comments

I have looked but can't seem to find a way to save the drawing and load it back in for later reuse.

Say i draw something, but instead of saving it as a UIImage i want to save each layer, and load them back in later, so i can erase or keep on drawing.

Tried extracting layer.sublayers and saving it and reloading everything, but it gets f*cked.

steelzeh avatar Nov 26 '16 21:11 steelzeh

This is indeed missing. I haven't looked a this project in a while but you could look into NSCoding. I'll try to see if I can add this next week.

e7711bbear avatar Nov 28 '16 02:11 e7711bbear

I am also trying to save the Layers to disk for reuse later. I thought that this would work for loading and saving, but something is not quite right:

for Saving: `func saveImage(imagePath: String, image: ATShapeLayer) { let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask) let documentsDirectoryURL = paths.first!.appendingPathComponent("PrivateDocuments") let imageURL = documentsDirectoryURL.appendingPathComponent(imagePath)

    do {
        let data = try NSKeyedArchiver.archivedData(withRootObject: image, requiringSecureCoding: false)
        try data.write(to: imageURL)
    } catch {
        print("couldn't save layer")
    }
    
}`

For loading: `func loadImage(image: String) -> ATShapeLayer { let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask) let documentsDirectoryURL = paths.first!.appendingPathComponent("PrivateDocuments") let imageURL = documentsDirectoryURL.appendingPathComponent(image) var newDrawing: ATShapeLayer? do { let codedData = try? Data(contentsOf: imageURL) if codedData == nil { let blankLayer = ATShapeLayer() saveImage(imagePath: image, image: blankLayer) newDrawing = blankLayer } else { newDrawing = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(codedData!) as? ATShapeLayer }

    } catch {
        print("couldn't load layer")
    }
    if newDrawing == nil {
        newDrawing = ATShapeLayer()
    }
    return newDrawing!
}`

When I tried as a CALayer it gives an error right away when loading from disk, and when saving.

jforward5 avatar Sep 09 '19 19:09 jforward5

This is indeed missing. I haven't looked a this project in a while but you could look into NSCoding. I'll try to see if I can add this next week.

Did you ever get this working?

jforward5 avatar Sep 10 '19 00:09 jforward5