我将UIWebView的图层渲染到图形上下文中,然后使用UIGraphicsBeginPDFPageWithInfo()系列函数将其包含在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();
另外,这是我看到的输出的屏幕截图:
最佳答案
我遇到了同样的问题。每当尝试将UIWebView呈现到PDF上下文中并且帧宽度> 512时,都会发生这种情况。我无法诊断出确切的问题,但是通过将UIWebView呈现为UIImage,然后将UIImage呈现为pdf来解决此问题。上下文。
代码为:
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0,0,kWidth,kHeight),nil);
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)];
关于iphone - 将UIWebView渲染为图像/PDF具有视觉效果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7585762/