Rendering UIImage/CGImage into CGPDFContext results in... blankness!

Posted by quixoto on Stack Overflow See other posts from Stack Overflow or by quixoto
Published on 2010-04-06T01:56:16Z Indexed on 2010/04/06 2:03 UTC
Read the original article Hit count: 588

Hi all, I'm trying to take an image that I have in a image object and render into a Core Graphics PDF context-- happens to be on an iPhone but this question surely applies equally to desktop Quartz. This UIImage is a simple color-on-white image at about 600x800 resolution. If I (say) turn it into a PNG file, that file looks exactly as expected-- so the data is OK.

Here's what I'm doing to generate the PDF:

NSMutableData * outputData = [[NSMutableData alloc] init];
CGDataConsumerRef dataConsumer = CGDataConsumerCreateWithCFData((CFMutableDataRef)outputData);

CFMutableDictionaryRef attrDictionary = NULL;    
attrDictionary = CFDictionaryCreateMutable(NULL, 0,
                                         &kCFTypeDictionaryKeyCallBacks,
                                         &kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(attrDictionary, kCGPDFContextTitle, @"My Awesome Document");
CGContextRef pdfContext = CGPDFContextCreate(dataConsumer, NULL, attrDictionary); 
CFRelease(dataConsumer);
CFRelease(attrDictionary);
CGImageRef pageImage = [myUIImage CGImage];
CGPDFContextBeginPage(pdfContext, NULL);
CGContextDrawImage(pdfContext, CGRectMake(0, 0, [myUIImage size].width, [myUIImage size].height), pageImage);
CGPDFContextEndPage(pdfContext);
CGContextRelease(pdfContext);

Resulting PDF, which ends up in outputData, seems like a valid PDF file (opens correctly, document title is present in metadata), but it consists of precisely one blank page.

What am I doing wrong?

Thanks.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about core-graphics