Take snapshot / screenshot of UIWebView

2019-03-22 04:53发布

How do I take a snapshot / screenshot of the UIWebView control?

标签: ios uiwebview
3条回答
SAY GOODBYE
2楼-- · 2019-03-22 05:11

use this code to take a screenshot and save it to the library

    UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, self, nil, nil);

you can change the screenshot taking portion by changing the

UIGraphicsBeginImageContext(self.view.bounds.size);

to your respective value try it it will help you

查看更多
SAY GOODBYE
3楼-- · 2019-03-22 05:22

The accepted answer is correct, however the screenshot is not in retina resolution. Using UIGraphicsBeginImageContextWithOptions and set its scale parameter 0.0f makes it capture in native resolution.

UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
CGRect rect = [keyWindow bounds];
UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
[keyWindow.layer renderInContext:context];   
UIImage *capturedScreen = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
查看更多
狗以群分
4楼-- · 2019-03-22 05:34

Use This:

UIGraphicsBeginImageContext(rect.size);
[webView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
查看更多
登录 后发表回答