Reputation: 4235
I've got NSView
and to this NSView
I added some subviews which are subclass of NSView
(named: Square
). Squares are 50x50 on different positions. I want to render this NSView
s with background color Red
or Blue
with this white background like on screenshot.
- (void)saveAsPDF
{
NSString *homeDirectory = NSHomeDirectory();
NSURL *fileURL = [NSURL fileURLWithPath:[homeDirectory stringByAppendingPathComponent:@"plik.pdf"]];
CGRect mediaBox = self.bounds;
CGContextRef pdfContext = CGPDFContextCreateWithURL((__bridge CFURLRef)(fileURL), &mediaBox, NULL);
CGPDFContextBeginPage(pdfContext, nil);
for (Square *square in squareGroup) {
[square.layer renderInContext:pdfContext];
}
CGPDFContextEndPage(pdfContext);
CGContextRelease(pdfContext);
Only what i've got is blank PDF file. How can i draw this squares correctly in pdfContext?
Upvotes: 1
Views: 1349
Reputation: 29498
You must call CGPDFContextClose for the PDF data to be written:
// ...
CGPDFContextEndPage(pdfContext);
// Close the PDF document
CGPDFContextClose(pdfContenxt);
CGContextRelease(pdfContext);
// ...
The documentation notes that closing the context causes data to be written, which might explain why you're getting a blank PDF without closing:
After closing the context, all pending data is written to the context destination, and the PDF file is completed. No additional data can be written to the destination context after the PDF document is closed.
Upvotes: 3