Skip to content

Instantly share code, notes, and snippets.

@zhaoyswd
Last active August 18, 2016 08:42
Show Gist options
  • Save zhaoyswd/6203836 to your computer and use it in GitHub Desktop.
Save zhaoyswd/6203836 to your computer and use it in GitHub Desktop.
UIWebView+Screenshot
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://stackoverflow.com/questions/10692221/screenshot-of-an-uiwebview-that-is-not-currently-on-screen"]]];
NSLog(@"webview = %@",self.webView);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSLog(@"contentSize = %f %f",webView.scrollView.contentSize.width,webView.scrollView.contentSize.height);
webView.frame = CGRectMake(0, 0, webView.scrollView.contentSize.width, webView.scrollView.contentSize.height);
UIGraphicsBeginImageContextWithOptions(webView.scrollView.contentSize, NO, 0);
[webView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *data = UIImagePNGRepresentation(image);
NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:@"page.png"];
[data writeToFile:path atomically:YES];
NSLog(@"path = %@",path);
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
NSLog(@"%s",__func__);
}
@nonstriater
Copy link

it's wrong,this will not work absolutely

@Clintlin
Copy link

After you take that, u could use "self.webView.scrollView.contentOffset = CGPointMake(0, currentOffset);" to scroll your webview down and take other one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment