Search code examples
iosuiwebviewuiimagescreenshotlayer

Screenshot UIWebView’s full content in IBAction


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?


Solution

  • 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.