tableview
我列出了以下文档文件 jpg、png、html、txt、doc、xls . didselect
tableview
的方法我导航到另一个 View Controller 以在 UIWebView
中加载这些文档文件. tableview
中的单元格图像添加的文档. 1.第一
ViewController
在表格 View 中- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([FileType isEqualToString:@"jpg"])
{
cell.imageView.image =[UIImage imageNamed:[NSString stringWithFormat:@"%@.jpg",[FilelistArray objectAtIndex:indexPath.row]]];
}
else if ([FileType isEqualToString:@"doc"])
{
cell.imageView.image = ??????
}
}
2.第二
ViewController
..- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *targetURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"FileName" ofType:@"doc"]];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL]
WebView.delegate = self;
[WebView setScalesPageToFit:YES];
CGFloat contentHeight = WebView.scrollView.contentSize.height;
[WebView loadRequest:request];
[self.view addSubview:WebView];
}
输出:
最佳答案
您可以在 UIWebView 中加载内容并使用以下方法捕获它:
- (UIImage*)screenshot:(UIView*)theView
{
UIGraphicsBeginImageContext(theView.bounds.size);
[theView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
存储返回的图像并将其用作缩略图。
您可以在用户第一次查看文档时生成它(在这种情况下,用户第一次看到列表时缩略图将不可用),或者在带有表格 View 的 View Controller 中生成它们。对于第二个选项,您必须创建一个 UIWebView 并将其放在不可见的 super View 的框架之外,然后捕获它。
现在我在想,也许还有另一种选择……在文档不是图像的情况下,只需使用一个小的 UIWebView 而不是图像,然后加载文档。它应该在单元格上显得很小。只要确保设置
scalesPageToFit
是的。编辑:
我很快尝试了我的最后一个建议,它奏效了。我创建了一个带有 UITableView 的 UIViewController,这是一个单元格,里面有一个带有标签 1000 的 UIWebView。然后我这样做:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"webViewTableCell"];
UIWebView *wv = (UIWebView *)[cell viewWithTag:1000];
wv.scalesPageToFit = NO;
wv.userInteractionEnabled = NO;
[wv loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://studentaid.ed.gov/sites/default/files/public-service-employment-certification-form.pdf"]]];
return cell;
}
它从 Internet 加载随机 PDF 并将其显示为缩略图:
关于ios - 如何为 iOS 的 UIWebView 中加载的文档(doc、html)创建缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23124603/