这是另一种直接归档/取消归档 UIBezierPath 的解决方案:
- (NSURL *)documentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
- (void)archivePath:(UIBezierPath*)bPath withName:(NSString *)archiveName
{
NSString *savePath = [[[self documentsDirectory] URLByAppendingPathComponent:archiveName] path];
[NSKeyedArchiver archiveRootObject:bPath toFile:savePath];
}
- (UIBezierPath *)unarchivePathWithName:(NSString *)archiveName
{
return (UIBezierPath *)[NSKeyedUnarchiver unarchiveObjectWithFile:[[[self documentsDirectory] URLByAppendingPathComponent:archiveName] path]];
}
我没有进行任何错误检查或验证文件是否存在,但如果您确定那里有文件,这将有效。以下是如何使用它的示例:
UIBezierPath *bez = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 500, 100)];
[bez appendPath:[UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 200, 200)]];
[self archivePath:bez withName:@"path.archive"];
UIBezierPath *bez2 = [self unarchivePathWithName:@"path.archive"];