0

I'm working on an OSX application where I need to store 446 CGLayers that get placed in a PDF context and am wondering if there's a way to write and read them from a file, rather than generating them when the application loads

I've read that CGLayer is no longer recommended, but I feel they really fit what I need. Also, if I use bitmapGraphicsContexts, they can pixelate when zooming in.

I am able to store them in NSArrays, both by storing them in NSValue and puting them into the array by bridging. I've also tried storing them in C arrays, but that didn't work out.

My problem comes when trying to store these arrays in a file. writeToFile: doesn't work with CGLayers, but NSKeyedArchiver/NSKeyedUnarchiver hasn't worked either, both when the layers are in NSValues or bridged.

Here's my method that attempts to write and read an array containing a single layer from a file.

+(CGLayerRef) colorAnnotations:(CGContextRef)context{    
float symbolSize = 8;
CGRect glyphBox = CGRectMake(0,0, 8, 8);    
CGLayerRef   annotationLayer = CGLayerCreateWithContext (context,glyphBox.size, NULL);
CGContextRef annotaionLayerContext = CGLayerGetContext(annotationLayer);

CGMutablePathRef annot = CGPathCreateMutable();

//Drawing annotation
/*...*/

NSMutableArray *test = [[NSMutableArray alloc]init];    
[test addObject:[[NSValue alloc] initWithBytes:&annotationLayer objCType:@encode(CGLayerRef)]];

//Getting file path in Documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/annots.data"];

[NSKeyedArchiver archiveRootObject:test toFile:dataPath];

NSMutableArray *testLoads = [NSKeyedUnarchiver unarchiveObjectWithFile:dataPath];

CGLayerRef layerToReturn;

[[testLoads objectAtIndex:0]getValue:&layerToReturn];

return layerToReturn;
}

I get [NSKeyedArchiver encodeValueOfObjCType:at:]: unknown type encoding ('^')') was raisedfrom this, pretty sure because of the CGLayerRef type.

The lines needed to draw the different annotations I need are pretty long, so I've been trying to figure out a way to have them made and stored in a file without having to make them on startup each time. So far I'm not seeing a way to do this, but was hoping someone here may know of one and would appreciate any help.

Community
  • 1
  • 1
nintandrew
  • 105
  • 1
  • 6
  • I read the first sentence (and nothing more) and thought "you aren't following MVC". The CGLayers are your 'V'. You want to be writing the 'M'. – trojanfoe Oct 31 '15 at 23:22
  • @trojanfoe These layers are each assigned to a specific color, so when the color is used in the PDF, the layer is also drawn. I'm thinking the PDF is my view, which I can draw to fine, I was just wondering if there's a way to archive an array of CGLayers. Thanks for looking! – nintandrew Nov 01 '15 at 02:15

0 Answers0