Is it possible to encode CGPathRef variables? I mean is there an encodeObject:forKey like method for CGPathRef variables?
yes, there is - I have done this: https://github.com/mro/MROGeometry/blob/master/NSCoder_MROCGPath.m
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.
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.
来源:https://stackoverflow.com/questions/1429426/cgpathref-encoding