如何将 CGPath 保存到文件

发布于 2024-12-24 02:53:25 字数 4958 浏览 3 评论 0原文

我在 CALayers 中绘制了大约 20 个简单形状,如下所示(在 CAShapelayer 中绘制的 CGPath)。现在我有一个非常长的文件,其中包含这种格式的所有 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];

I have around 20 simple shapes drawn inside CALayers like the one below (CGPaths drawn in a CAShapelayer). Right now I have a really long file with all 20 shapes in this format. When the parent UIView is created it loads one of these shapes, based on what was selected in a previous screen.This is working fine, but maintaining the shape code is way too cumbersome. What would be the best way to take the block of code below and store it into an individual file that I could call up and execute when needed?(ie: 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];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠檬色的秋千 2024-12-31 02:53:25

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

Both UIBezierPath (iOS) and NSBezierPath (Mac) support the NSCoding protocol. If you wrap your path with one of these classes, it should be trivial to save and restore.

婴鹅 2024-12-31 02:53:25

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

Another possibility would be to save the graphics as a PDF file, which can be edited graphically. You could then generate a CGImage out of the PDF and draw it on screen.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文