我正在开发一个 OSX 应用程序,我需要存储 446 个放置在 PDF 上下文中的 CGLayer,我想知道是否有办法从文件中写入和读取它们,而不是在应用程序加载时生成它们
我读过CGLayer 不再被推荐,但我觉得它们真的很适合我的需要。此外,如果我使用 bitmapGraphicsContexts,它们会在放大时像素化。
我可以将它们存储在 NSArrays 中,既可以将它们存储在 NSValue 中,也可以通过bridging 将它们放入数组中。我也尝试将它们存储在 C 数组中,但没有成功。
尝试将这些数组存储在文件中时出现了我的问题。writeToFile: 不适用于 CGLayers,但 NSKeyedArchiver/NSKeyedUnarchiver 也不起作用,无论是在图层位于 NSValues 中还是桥接时。
这是我尝试从文件中写入和读取包含单个层的数组的方法。
+(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;
}
我从中得到[NSKeyedArchiver encodeValueOfObjCType:at:]: unknown type encoding ('^')') was raised
,很确定是因为 CGLayerRef 类型。
绘制我需要的不同注释所需的行很长,所以我一直试图找出一种方法来制作它们并将它们存储在一个文件中,而不必每次启动时都制作它们。到目前为止,我还没有看到一种方法来做到这一点,但希望这里的某个人可能知道一个并且会感谢任何帮助。