Is it possible to encode CGPathRef variables? I mean is there an encodeObject:forKey like method for CGPathRef variables?
Asked
Active
Viewed 3,041 times
3 Answers
2
yes, there is - I have done this: https://github.com/mro/MROGeometry/blob/master/NSCoder_MROCGPath.m

Marcus Rohrmoser
- 342
- 2
- 9
-
Thank you so much for this. Life saver! – anna Aug 09 '12 at 03:28
-
Oh no, the only link that was about to save my life is broken now. – 0xNSHuman Oct 13 '14 at 22:14
-
here it is: https://github.com/mro/MROGeometry/blob/master/MROGeometry/NSCoder_MROCGPath.m – Marcus Rohrmoser Jul 30 '15 at 10:09
-
It seems as though it won't handle all paths yet? There's a comment that it doesn't support "eliptical arcs". – Glenn Howes Oct 15 '15 at 13:52
-
Also, the license is fairly political. – Glenn Howes Oct 15 '15 at 13:57
1
CGPaths are objects, but I don't think you can encode them—at least, if you can, it's not documented. You'll need to generate a saveable representation of the path yourself, and encode that, and then, when you decode that representation, reincarnate the path from it yourself.

Peter Hosey
- 95,783
- 15
- 211
- 370
-
Is there any relationship between `CGPathRef` and `NSBezierPath`? `NSBezierPath` should be serializable, and if there's a way to convert one to the other, you could encode the `NSBezierPath` and convert to a CGPath as needed. `NSBezierPath` strikes me as a bit less opaque than CGPath. – Alex Sep 16 '09 at 13:17
-
2No, there is none. You would have to convert manually using an applier function. This is usually not that hard, but if the CGPath contains a quadratic curve (one with one control point), you'd have to convert that to a cubic curve (two control points). – Peter Hosey Sep 16 '09 at 13:40
-
On iOS, you can `UIBezierPath`, which is `NSCoding` compliant and which implements a class method `bezierPathWithCGPath:`. See http://stackoverflow.com/questions/9938864/encode-and-decode-cgmutablepathref-with-arc/23548794#23548794 – algal May 08 '14 at 17:36
1
This header on Github has two utility strings to interchange CGPaths with NSStrings:
+(nullable NSString*) svgPathFromCGPath:(CGPathRef)aPath;
+(nullable CGPathRef) newCGPathFromSVGPath:(NSString*)anSVGPath whileApplyingTransform:(CGAffineTransform)aTransform;
You can use those strings with NSCoders to store things away. Be warned that anytime you convert between paths and strings you might lose significant digits because of the conversion.
I wrote these routines and they are under MIT license.

Glenn Howes
- 5,447
- 2
- 25
- 29