我有一组本地 HTML 页面,我想动态批量生成缩略图(我只想显示缩略图,而不是完整的网页)。这是我完成此操作的方式:
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
然后在委托中:
- (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;
}
以下是我的问题:
1)我的一般方法是否正确?这是动态批处理网页缩略图的最有效方法吗?
2) 我希望能够从屏幕外的 UIWebView 呈现缩略图,但除非我将 UIWebView 添加到视图层次结构中,否则不会调用 webViewDidFinishLoad:。有没有办法避免这种情况?
3) 如果我尝试在 webViewDidFinishLoad: 中捕获 UIWebView 的图像,我会得到一个空白图像。我必须人为地延迟捕获才能工作。有什么办法吗?
谢谢!