0

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];
Frank
  • 299
  • 1
  • 4
  • 11

2 Answers2

6

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.

Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
  • 1) How would I wrap the code above with UIBezierPath (would I have to convert EACH cgpathref above) 2)While searching for info on #1 I keep finding things that say UIBezierPath wraps CGPath by default (if that is the case, is it already wrapped? how would I do the save and restore? – Frank Jan 04 '12 at 18:22
  • 1
    You can either create a `UIBezierPath` instance from an existing `CGPathRef` or create an empty `UIBezierPath` and translate your code above to the object-oriented syntax. The docs for `UIBezierPath` clearly show the options you have (incl. setting properties like line width and miter limit). For questions regarding archiving and unarchiving, read Apple's Archives and Serializations Programming Guide: http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Archiving/Archiving.html – Ole Begemann Jan 04 '12 at 21:20
  • For swift: Here is a solution that converts CGPath instances to an UIBezierPath instance, or any other path format you like: http://stackoverflow.com/questions/24274913/equivalent-of-or-alternative-to-cgpathapply-in-swift (It works) – Sentry.co Aug 09 '16 at 13:29
-2

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.

GorillaPatch
  • 5,007
  • 1
  • 39
  • 56