将 UIWebView 渲染为图像/PDF 具有视觉伪影
我将 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();
另外,这是我所看到的输出的屏幕截图:
I'm rendering a UIWebView's layer into a graphics context and then using the UIGraphicsBeginPDFPageWithInfo() family of functions to include it in a PDF.
My problem is that the output includes an extra set of gray lines that aren't part of my data set. I'm hoping someone can shed some light on where they're coming from.
An example of the output is included below. The HTML document that is being rendered contains nothing but the text 'THIS IS A TEST' - the boxes you see are coming from the rendering process somewhere. When rendered on the screen, it's just black text on a white screen - no lines/boxes.
Anyone have any ideas what's going on? Thanks!
Here's the code I'm using to render this web view as a PDF:
NSMutableData *pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, nil);
CGRect viewBounds = webView.bounds;
UIGraphicsBeginPDFPageWithInfo(viewBounds, nil);
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
[webView.layer renderInContext:pdfContext];
UIGraphicsEndPDFContext();
Also, here's a screenshot of what I'm seeing for output:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我遇到了同样的问题。每当尝试将 UIWebView 渲染到 PDF 上下文中并且框架宽度 > 时,就会发生这种情况。 512. 我无法诊断确切的问题,但通过将 UIWebView 渲染到 UIImage 中,然后将 UIImage 渲染到 pdf 上下文中来解决这个问题。
代码为:
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, kWidth, kHeight), nil);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
I ran into the same issue. It would happen whenever trying to render the UIWebView into the PDF context, and frame width > 512. I was not able to diagnose the exact issue, but worked around it by rendering the UIWebView into a UIImage, and then rendering the UIImage into the pdf context.
Code as:
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, kWidth, kHeight), nil);
CGContextRef currentContext = UIGraphicsGetCurrentContext();