What is the best way to save a CGMutablePath as JSON data that can be uploaded to backend?
I'm aware that it could be converted into a Data object using UIBezierPath which conforms to NSCoding, then the data could be converted again to a string and saved to backend, but that doesn't seem like a great way. Is there a better way to save this object to backend?
Perhaps you could pull out the huge array of points that make up the path, convert it into a string and save that. Would this be best?
A
CGPathorCGMutablePathis a very simple data structure. It's an array of path elements. An each path elements is either a move, line, cubicCurve, curve or closeSubpath operation with 0 to 3 points. That's all. There are no additional attributes or variants.So it's quite straight-forward to translate a path into an array of path element (
struct PathElement) and then encode it as JSON. It results in a JSON that can be easily read with any programming language and works on many graphics systems (incl. iOS/macOS Quartz, Postscript, PDF, Windows GDI+).The output of the below sample code consists of the printed
CGMutablePath, the generated JSON and the path restored from JSON:Sample code: