ATSketchKit
ATSketchKit copied to clipboard
Save draft?
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.
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.
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.
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?