CGPathRef encoding

2020-07-18 10:42发布

问题:

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

回答1:

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



回答2:

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.



回答3:

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.