2012-01-04 11 views
0

私は、以下のようなCALayers内に描かれた約20個のシンプルな形を持っています(CAShapelayerで描かれたCGPaths)。今、私はこのフォーマットで20種類のすべての図形を持つ本当に長いファイルを持っています。親UIViewが作成されると、前の画面で選択されたものに基づいて、これらの図形の1つが読み込まれます。これはうまくいきますが、シェイプコードを維持するのは面倒です。以下のコードブロックをとり、必要に応じて呼び出すことができる個々のファイルに格納する最良の方法は何でしょうか(つまり:star.txt、apple.txt、moon.txt、tree.txt)。CGPathをファイルに保存する方法

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]; 

答えて

5

両方UIBezierPath(iOS版)とNSBezierPath(Mac)をNSCodingプロトコルをサポートしています。これらのクラスの1つでパスをラップする場合は、保存して復元するのは簡単です。

+0

1)上記のコードをUIBezierPathで囲みたいのですが(上記のcgpathrefを変換する必要があります)2)#1の情報を検索している間、UIBezierPathがデフォルトでCGPathをラップする – Frank

+1

既存の 'CGPathRef'から' UIBezierPath'インスタンスを作成することも、空の 'UIBezierPath'を作成して上のコードをオブジェクト指向の構文に変換することもできます。 'UIBezierPath'のドキュメントには、あなたが持っているオプション(線幅やマイター限界などのプロパティを含む)が明記されています。アーカイブやアーカイブ解除に関する質問は、AppleのArchives and Serializations Programming Guide:http://developer.apple.com/ライブラリ/ ios /#documentation/Cocoa/Conceptual/Archiving/Archiving.html –

+0

swiftの場合:CGPathインスタンスを変換するソリューションですUIBezierPathインスタンス、またはあなたが好きな他のパスフォーマットに変更します:http://stackoverflow.com/questions/24274913/equivalent-of-or-alternative-to-cgpathapply-in-swift(それは動作します) – eonist

-2

グラフィックスをPDFファイルとして保存して、グラフィックで編集することもできます。その後、PDFからCGImageを生成し、それを画面に描画することができます。

関連する問題