Hello currently I have the code in an IBAction which saves a screenshot of the UIWebView, what I want it for it to take a screenshot of the full content of the webpage (both what is visible and what is not)

So far I have managed to get it to take a screenshot of the size of the full content with the visible content showing, however the rest of the screenshot where the non-visible content is is white.

CGSize layerSize = [_myWebView sizeThatFits:myWebView.scrollView.contentSize]; if ([UIScreen instancesRespondToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2.0f) { UIGraphicsBeginImageContextWithOptions(layerSize, NO, 2.0f); UIGraphicsBeginImageContext(layerSize); } else { UIGraphicsBeginImageContext(layerSize); }

[_myWebView.scrollView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, self, nil, nil); 

What can I do to make all the content visible in the screenshot?

有帮助吗?

解决方案

This is a guess:
Try adjusting the frame.size of the webView to be equal to the scrollView contentSize. Then, reload the webview and call renderInContext: after the reload is complete. See what that gives you.

其他提示

If the frame of the webview is equal to the superview(screen) then the webview wont render the content which is out of the bounds. So You have to set the frame of webview to its contentSize and take a screenshot.

ContentSize can be obtained only after the webView is loaded. So in the webviewDidFinishLoad get the height of the webview's contentsize.

CGFloat webViewHeight; 

-(void)webViewDidFinishLoad:(UIWebView *)webView{

   webViewHeight = webView.scrollView.contentSize.height;

}

Then set the webview height to webview contentsize as follows

webView.frame = CGRectMake(webView.frame.origin.x,webView.frame.origin.y,webView.frame.size.width,webViewHeight);

then use

 UIGraphicsBeginImageContext(webview.frame.size);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top