Create thumbnails of local web pages in iOS

I have a set of local HTML pages that I would like batch thumbnails to display on the fly (I want to show only thumbnails, not full web pages). So I achieve this:

NSString* path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:link]; NSURL* url = [NSURL fileURLWithPath:path]; NSURLRequest* request = [NSURLRequest requestWithURL:url]; UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 725, 1004)]; webView.delegate = cell; [webView loadRequest:request]; [self.view addSubview:webView]; // doesn't work without this line, but then the UIWebView is onscreen 

Then in the delegate:

 - (void)webViewDidFinishLoad:(UIWebView *)webView { [self performSelector: @selector(render:) withObject: webView afterDelay: 0.01f]; } - (void) render: (id) obj { UIWebView* webView = (UIWebView*) obj; CGSize thumbsize = CGSizeMake(96,72); UIGraphicsBeginImageContext(thumbsize); CGContextRef context = UIGraphicsGetCurrentContext(); CGFloat scalingFactor = thumbsize.width/webView.frame.size.width; CGContextScaleCTM(context, scalingFactor,scalingFactor); [webView.layer renderInContext: context]; UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); self.thumbnail.image = resultImage; } 

Here are my questions:

1) Is my general approach correct? Is this the most efficient way to batch process thumbnails of web pages on the fly?

2) I want to be able to display a thumbnail from an external UIWebView screen, but webViewDidFinishLoad: is not called unless I add a UIWebView to the view hierarchy. Is there any way to avoid this?

3) If I try to capture a UIWebView image in webViewDidFinishLoad :, I get a blank image. I have to put an artificial delay for the capture to work. How to get around this?

Thanks!

+4
source share
1 answer

1) This seems to be the only way I know this without relying on a third-party server API (if one exists)

2) You can take the UIWebView off the screen by setting its frame to a position that is outside the screen. For example, 320 568 725 1008. I have not tested it, but you can even set the view to be a 1 x 1px frame.

3) I assume that this is because when calling webViewDidFinishLoad: in the web view, SetNeedsDisplay () is called, but the web view is not yet redrawn. I am not sure about that.

+1
source

All Articles