我正在研究一个React Native项目,该项目推迟了对上下文的进一步写入操作。保存PDF上下文并推迟drawText操作进行单独的调用时,PDF大小为零。
如何保存图形上下文,以便可以使用进一步的方法调用对其进行写入?
- (id)initWithPath:(NSString *)path {
if (self = [self init]) {
RCTLog(@"PDFDocument.initWithPath: %@", path);
UIGraphicsBeginPDFContextToFile(path, CGRectZero, nil);
context = UIGraphicsGetCurrentContext();
UIGraphicsPopContext();
}
return self;
}
- (void)text:(NSString*)text {
RCTLog(@"PDFDocument.text: %@", text);
UIGraphicsPushContext(context);
UIGraphicsBeginPDFPage();
[[UIColor blackColor] setFill];
[text drawAtPoint:CGPointZero withAttributes:nil];
UIGraphicsPopContext();
}
- (void)close {
RCTLog(@"PDFDocument.close");
UIGraphicsPushContext(context);
UIGraphicsEndPDFContext();
}
- (void)create {
PDFDocument *doc = [[PDFDocument alloc] initWithPath:path];
[doc text:@"Hello World!"];
[doc close];
}