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];
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!
source share