将UIImage/CGImage渲染到CGPDFContext中会导致...空白! [英] Rendering UIImage/CGImage into CGPDFContext results in... blankness!
问题描述
我正在尝试拍摄图像对象中包含的图像,并渲染到Core Graphics PDF上下文中-碰巧是在iPhone上,但是这个问题肯定适用于台式Quartz.此UIImage是一个简单的彩色白色图像,分辨率约为600x800.如果我(说)将其转换为PNG文件,则该文件看起来将与预期的完全一样-这样数据就可以了.
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.
这是我生成PDF的工作:
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);
CGPDFContextClose(pdfContext);
CGContextRelease(pdfContext);
生成的以outputData
结尾的PDF看起来像是有效的PDF文件(正确打开,元数据中包含文档标题),但是它恰好由一个空白页组成.
The 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.
我在做什么错了?
谢谢.
更新:哈!这是我的错我仅用于生成PNG文件的测试代码通过了另一条路径来获取数据. PDF路径确实接收到空白图像.
UPDATE: Ha! This was my fault. My test code for just generating the PNG file went through a different path to get the data. The PDF path indeed was receiving an empty image.
推荐答案
我测试了您的代码,它似乎可以正常工作.
将UIImage绘制到上下文中时,您确定您的UIImage是有效的,而不是nil吗?
我的测试方法是从主捆绑包中加载.png文件,然后将最终的pdf文件写入应用程序文档文件夹中的文件中:
I tested your code and it seems to work.
Are you sure your UIImage is valid and not nil when drawing it into the context?
My test method loads a .png from the main bundle and writes the final pdf to a file in the applications document folder:
- (IBAction)outputPDF:(id)sender
{
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);
UIImage* myUIImage = [UIImage imageNamed:@"tmp.png"];
CGImageRef pageImage = [myUIImage CGImage];
CGPDFContextBeginPage(pdfContext, NULL);
CGContextDrawImage(pdfContext, CGRectMake(0, 0, [myUIImage size].width, [myUIImage size].height), pageImage);
CGPDFContextEndPage(pdfContext);
CGPDFContextClose(pdfContext);
CGContextRelease(pdfContext);
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* appFile = [documentsDirectory stringByAppendingPathComponent:@"tmp.pdf"];
[outputData writeToFile:appFile atomically:YES];
}
这篇关于将UIImage/CGImage渲染到CGPDFContext中会导致...空白!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!