3

Is it possible to encode CGPathRef variables? I mean is there an encodeObject:forKey like method for CGPathRef variables?

cocoatoucher
  • 1,483
  • 3
  • 17
  • 22

3 Answers3

2

yes, there is - I have done this: https://github.com/mro/MROGeometry/blob/master/NSCoder_MROCGPath.m

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
  • 2
    No, 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