【问题标题】:how to save a CGPath to a file如何将 CGPath 保存到文件中
【发布时间】:2012-01-04 16:37:30
【问题描述】:

我在 CALayers 中绘制了大约 20 个简单的形状,如下图(在 CAShapelayer 中绘制的 CGPaths)。现在我有一个非常长的文件,其中包含这种格式的所有 20 种形状。创建父 UIView 时,它会根据前一个屏幕中选择的内容加载其中一个形状。这工作正常,但维护形状代码太麻烦了。将下面的代码块存储到我可以在需要时调用和执行的单个文件中的最佳方法是什么?(即:star.txt、apple.txt、moon.txt、tree.txt):

CALayer* root = [[CALayer alloc] init];
root.name = nil;
root.bounds = CGRectMake(0.000000, 0.000000, 768.000000, 768.000000);
root.frame = CGRectMake(0.000000, 0.000000, 768.000000, 768.000000);
[root addSublayer:root];
[root release];
CALayer* root_layer1 = [[CALayer alloc] init];
root_layer1.name = nil;
root_layer1.bounds = CGRectMake(0.000000, 0.000000, 768.000000, 768.000000);
root_layer1.frame = CGRectMake(0.000000, 0.000000, 768.000000, 768.000000);
[someUIView.layer addSublayer:root_layer1];
[root_layer1 release];
CAShapeLayer* root_layer1_layer2 = [[CAShapeLayer alloc] init];
CGMutablePathRef root_layer1_layer2_path_pathref = CGPathCreateMutable();
CGPathMoveToPoint(root_layer1_layer2_path_pathref, NULL, 415.493011, 49.774002);
CGPathAddLineToPoint(root_layer1_layer2_path_pathref, NULL, 448.989014, 153.523010);
CGPathAddCurveToPoint(root_layer1_layer2_path_pathref, NULL, 458.575012, 183.251007, 485.170044, 202.583008, 516.384033, 202.510010);
CGPathAddLineToPoint(root_layer1_layer2_path_pathref, NULL, 625.388062, 202.293015);
CGPathAddCurveToPoint(root_layer1_layer2_path_pathref, NULL, 656.602051, 202.221008, 683.138062, 221.466003, 692.754028, 251.121002);
CGPathAddCurveToPoint(root_layer1_layer2_path_pathref, NULL, 702.427002, 280.804993, 692.263000, 311.960999, 666.968018, 330.239014);
CGPathAddLineToPoint(root_layer1_layer2_path_pathref, NULL, 578.668030, 394.154022);
CGPathAddCurveToPoint(root_layer1_layer2_path_pathref, NULL, 553.344055, 412.461029, 543.209045, 443.704010, 552.911011, 473.402039);
CGPathAddLineToPoint(root_layer1_layer2_path_pathref, NULL, 586.781006, 577.006042);
CGPathAddCurveToPoint(root_layer1_layer2_path_pathref, NULL, 596.483032, 606.632019, 586.377014, 637.818054, 561.169006, 656.152039);
CGPathAddCurveToPoint(root_layer1_layer2_path_pathref, NULL, 535.932983, 674.488037, 503.158997, 674.460022, 477.981018, 656.066040);
CGPathAddLineToPoint(root_layer1_layer2_path_pathref, NULL, 389.881012, 591.846069);
CGPathAddCurveToPoint(root_layer1_layer2_path_pathref, NULL, 364.645020, 573.423096, 331.842010, 573.423096, 306.546997, 591.846069);
CGPathAddLineToPoint(root_layer1_layer2_path_pathref, NULL, 218.506989, 656.066040);
CGPathAddCurveToPoint(root_layer1_layer2_path_pathref, NULL, 193.298981, 674.460022, 160.525986, 674.488037, 135.287994, 656.152039);
CGPathAddCurveToPoint(root_layer1_layer2_path_pathref, NULL, 110.079987, 637.817017, 99.973999, 606.631042, 109.675995, 577.006042);
CGPathAddLineToPoint(root_layer1_layer2_path_pathref, NULL, 143.546997, 473.402039);
CGPathAddCurveToPoint(root_layer1_layer2_path_pathref, NULL, 153.276993, 443.704041, 143.112991, 412.461029, 117.789993, 394.154053);
CGPathAddLineToPoint(root_layer1_layer2_path_pathref, NULL, 29.460999, 330.239990);
CGPathAddCurveToPoint(root_layer1_layer2_path_pathref, NULL, 4.194000, 311.961975, -5.970001, 280.804993, 3.674999, 251.122009);
CGPathAddCurveToPoint(root_layer1_layer2_path_pathref, NULL, 13.320000, 221.467010, 39.856003, 202.222015, 71.041000, 202.294006);
CGPathAddLineToPoint(root_layer1_layer2_path_pathref, NULL, 180.074005, 202.511002);
CGPathAddCurveToPoint(root_layer1_layer2_path_pathref, NULL, 211.288010, 202.583008, 237.882019, 183.251007, 247.497009, 153.524002);
CGPathAddLineToPoint(root_layer1_layer2_path_pathref, NULL, 280.964020, 49.775002);
CGPathAddCurveToPoint(root_layer1_layer2_path_pathref, NULL, 290.523010, 20.106003, 317.029022, 0.789001, 348.243011, 0.789001);
CGPathAddCurveToPoint(root_layer1_layer2_path_pathref, NULL, 379.398987, 0.787998, 405.936005, 20.105000, 415.493011, 49.774002);
root_layer1_layer2.path = root_layer1_layer2_path_pathref;
CGColorSpaceRef root_layer1_layer2_fillColor_colorref_colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat root_layer1_layer2_fillColor_colorref_colorComponents[] = {0.858824,0.529412,0.725490,1.000000};
CGColorRef root_layer1_layer2_fillColor_colorref = CGColorCreate(root_layer1_layer2_fillColor_colorref_colorSpace, root_layer1_layer2_fillColor_colorref_colorComponents);
root_layer1_layer2.fillColor = root_layer1_layer2_fillColor_colorref;
root_layer1_layer2.fillRule = @"non-zero";
root_layer1_layer2.strokeColor = 0;
root_layer1_layer2.lineWidth = 1.000000;
root_layer1_layer2.miterLimit = 10.000000;
root_layer1_layer2.lineCap = @"butt";
root_layer1_layer2.lineJoin = @"miter";
root_layer1_layer2.lineDashPhase = 0.000000;
root_layer1_layer2.lineDashPattern = nil;
root_layer1_layer2.name = nil;
root_layer1_layer2.bounds = CGRectMake(0.000000, 0.000000, 697.000000, 670.000000);
root_layer1_layer2.frame = CGRectMake(36.000000, 40.000000, 697.000000, 670.000000);
[root_layer1 addSublayer:root_layer1_layer2];
[root_layer1_layer2 release];

【问题讨论】:

    标签: objective-c core-graphics cgpath


    【解决方案1】:

    UIBezierPath (iOS) 和 NSBezierPath (Mac) 都支持 NSCoding 协议。如果你用这些类之一包装你的路径,保存和恢复应该很简单。

    【讨论】:

    • 1) 我如何用 UIBezierPath 包装上面的代码(我是否必须转换上面的每个 cgpathref)2)在搜索 #1 的信息时,我一直在寻找说 UIBezierPath 默认包装 CGPath 的东西(如果是这种情况,它是否已经包装好?我将如何进行保存和恢复?
    • 您可以从现有的CGPathRef 创建一个UIBezierPath 实例,也可以创建一个空的UIBezierPath 并将上面的代码转换为面向对象的语法。 UIBezierPath 的文档清楚地显示了您拥有的选项(包括设置线宽和斜接限制等属性)。有关归档和取消归档的问题,请阅读 Apple 的归档和序列化编程指南:developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/…
    • 对于 swift:这是一个将 CGPath 实例转换为 UIBezierPath 实例或您喜欢的任何其他路径格式的解决方案:stackoverflow.com/questions/24274913/…(有效)
    【解决方案2】:

    另一种可能性是将图形保存为 PDF 文件,可以进行图形编辑。然后,您可以从 PDF 中生成 CGImage 并将其绘制在屏幕上。

    【讨论】:

      猜你喜欢
      • 2021-11-29
      • 1970-01-01
      • 2011-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多