嘗試,
{
//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