Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save draft? #28

Open
steelzeh opened this issue Nov 26, 2016 · 3 comments
Open

Save draft? #28

steelzeh opened this issue Nov 26, 2016 · 3 comments
Milestone

Comments

@steelzeh
Copy link

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.

@e7711bbear
Copy link
Owner

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 e7711bbear added this to the 0.1 milestone Nov 28, 2016
@jforward5
Copy link

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
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants