我有一組本地HTML頁面,我希望批量生成縮略圖(即我只想顯示縮略圖,而不是完整的網頁)。這是我實現這個方式:在iOS上生成本地網頁的縮略圖
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呈現縮略圖,但是webViewDidFinishLoad:除非將UIWebView添加到視圖層次結構,否則不會被調用。有沒有辦法可以避免這種情況? 3)如果我試圖在webViewDidFinishLoad中捕獲UIWebView的圖像:,我得到一個空白圖像。我必須爲捕捉工作延遲一段時間。任何方式在這個?
謝謝!