Reputation: 32066
I'd like to draw an full page image to a PDF but I've always had a hard time wrapping my head around CGContextRef
s so I don't know what to make of the error.
Note, this is NOT iOS. I'm making a desktop application.
So far, I have this:
-(void) addImage:(NSURL*) url toPage:(size_t) page toPDF:(CGPDFDocumentRef) pdf
{
NSImage *image = [[NSImage alloc] initWithContentsOfURL:url];
image = [image imageScaledToFitSize:pageSize.size]; //From Matt Gemmell's Crop extensions category
[image lockFocus];
CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
[image drawInRect:pageSize];
[image unlockFocus];
CGPDFPageRef pageRef = CGPDFDocumentGetPage(pdf, page);
CGPDFContextBeginPage(context, pageInformation);
CGContextDrawPDFPage(context, pageRef);
CGPDFContextEndPage(context);
}
However, I'm greeted with the error:
CGPDFContextEndPage: invalid context 0x61000017b540. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
What is wrong with my contexts please?
Upvotes: 0
Views: 669
Reputation: 119031
You need to create and use a CGPDFContext
. Different contexts are specific to different rendering processes / destinations so you need to choose the correct one. So look at using CGPDFContextCreateWithURL
to create a PDF context to write the data to a file.
Upvotes: 1