开发者

Rendering UIWebView as Image/PDF Has Visual Artifacts

开发者 https://www.devze.com 2023-04-08 03:43 出处:网络
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.

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 re开发者_StackOverflow社区nderInContext:pdfContext];

UIGraphicsEndPDFContext();

Also, here's a screenshot of what I'm seeing for output:

Rendering UIWebView as Image/PDF Has Visual Artifacts


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();

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)];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号