Take snapshot / screenshot of UIWebView

喜夏-厌秋 提交于 2020-01-01 03:42:05

问题


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


回答1:


Use This:

UIGraphicsBeginImageContext(rect.size);
[webView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();



回答2:


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



回答3:


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



来源:https://stackoverflow.com/questions/3458085/take-snapshot-screenshot-of-uiwebview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!