I am trying to create thumbnails of several pages that will be displayed through UIWebView. The problem is that all the images look gray or the background color that I set for UIWebView. Maybe I do not allow web browsing enough time to load the page?
I believe that I want to do the following:
- Create a UIWebView that does not appear on the screen
- Setting the graphics context
- For each page:
- Call loadRequest on the web page
- Display web view in graphical context
- capture screenshot
- Final graphic context
Here is the code that I have for one image:
UIWebView *screenWebView = _screenshotWebView;
UIView *screenView = _screenshotView;
if ([UIScreen instancesRespondToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2.0f) {
UIGraphicsBeginImageContextWithOptions(screenView.bounds.size, NO, 2.0f);
} else {
UIGraphicsBeginImageContext(screenView.bounds.size);
}
[screenWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:path ofType:@"html"]isDirectory:NO]]];
[screenView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
_screenshotWebView _screenshotView XIB , , , , .
? !
. , , , - : https://github.com/rmcl/webview_screenshot