2011-09-28 50 views
1

我将UIWebView的图层渲染为图形上下文,然后使用UIGraphicsBeginPDFPageWithInfo()系列函数将其包含在PDF中。将UIWebView渲染为图像/ PDF具有视觉伪像

我的问题是,输出包括一组额外的灰线,不是我的数据集的一部分。我希望有人能够说明他们来自哪里。

输出示例如下。正在呈现的HTML文档除了文本“这就是测试”之外什么也不包含 - 你看到的盒子来自某处的渲染过程。在屏幕上呈现时,它只是白色屏幕上的黑色文本 - 没有行/框。

任何人有任何想法发生了什么?谢谢!

下面是我使用呈现此Web视图作为PDF的代码:

NSMutableData *pdfData = [NSMutableData data]; 

UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, nil); 
CGRect viewBounds = webView.bounds; 

UIGraphicsBeginPDFPageWithInfo(viewBounds, nil); 

CGContextRef pdfContext = UIGraphicsGetCurrentContext(); 
[webView.layer renderInContext:pdfContext]; 

UIGraphicsEndPDFContext(); 

而且,这里是我所看到的输出的屏幕截图:

enter image description here

+0

可以共享数据集(样本pdf)文件吗? – nhisyam

回答

0

我遇到了同样的问题。每当试图将UIWebView渲染到PDF上下文并且帧宽> 512时,就会发生这种情况。我无法诊断确切的问题,但是通过将UIWebView渲染为UIImage,然后将UIImage渲染为pdf上下文。

代码为:

UIGraphicsBeginPDFPageWithInfo(CGRectMake(0,0,kWidth,kHeight),无);

CGContextRef currentContext = UIGraphicsGetCurrentContext();

UIImage* image = nil; 
UIGraphicsPushContext(currentContext); 
UIGraphicsBeginImageContext(self.webview.frame.size); 
{ 
    [self.webview.layer renderInContext: UIGraphicsGetCurrentContext()]; 
    image = UIGraphicsGetImageFromCurrentImageContext(); 
} 
UIGraphicsEndImageContext(); 
UIGraphicsPopContext(); 

[image drawInRect:CGRectMake(0, 0, kWidth, kHeight)];