尝试,
{
//read the pdf file
CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("iPhoneAppProgrammingGuide.pdf"), NULL, NULL);
PDFfile = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
CFRelease(pdfURL)
CGPDFPageRef page = CGPDFDocumentGetPage(PDFfile,currentpage);
context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextFillRect(context,self.bounds);
CGContextTranslateCTM(context, -1.0, [self bounds].size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(page, kCGPDFArtBox, [self bounds], 0, true));
CGContextDrawPDFPage(context, page);
CGContextRestoreGState(context);
//CGAffineTransform transform = aspectFit(CGPDFPageGetBoxRect(page, kCGPDFMediaBox),
CGContextGetClipBoundingBox(context));
// CGContextConcatCTM(context, transform);
UIGraphicsBeginImageContext(CGSizeMake(self.bounds.size.width, self.bounds.size.height));
}
感谢迈克尔!那么,你确定在这种情况下不能使用'renderInContext:'吗?我正在考虑“UIView”有多彩的背景或其他东西,这很难画出来。 – Akshay
是的,我确定。实际上,我在不久之前经历了这个问题,并且我的逻辑开始完全在你的位置(renderInContext :),然后我尝试搜索一些代码以将UIView绘制到PDF中,最后真正做到这一点的唯一方法是使用CGContext方法并从头开始绘制CGContextRef中的所有内容。 –
再次感谢迈克尔!那么,如果UIView具有渐变背景,您如何绘制?另外,我的实际使用案例是从'UIWebView'创建一个PDF,用于显示一个文件(比如ppt或doc),为此我发现文本和图像不是子视图(没有UILabels或UITextViews)。你也试过这个吗?对不起,最初没有指定。 – Akshay