问题
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