如何为iOS中的UIWebView中加载的文档(doc,html)创建缩略图

时间:2014-04-17 04:28:14

标签: ios objective-c uiwebview thumbnails

  • tableview列表中列出文件 jpg,png,html,txt,doc,xls
  • 我有jpg,png jpeg等图像文件的缩略图......
  • didselect的{​​{1}}方法导航到另一个视图控制器,以便在tableview中加载这些文档文件。
  • 任何人都可以告诉如何创建要在UIWebView中为单元格图像添加的文档的缩略图

1. Tableview中的第一个tableview

ViewController

2.Second - (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 = ?????? } } ..

ViewController

输出:

enter image description here

1 个答案:

答案 0 :(得分:3)

您可以在UIWebView中加载内容并使用以下内容捕获它:

- (UIImage*)screenshot:(UIView*)theView
{
    UIGraphicsBeginImageContext(theView.bounds.size);
    [theView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

存储返回的图像并将其用作缩略图。 您可以在用户第一次查看文档时生成它(在这种情况下,用户第一次看到列表将无法使用缩略图),或者在视图控制器中使用表格视图生成它们。对于第二个选项,您必须创建一个UIWebView并将其放在superview的框架之外,在那里它不可见,然后捕获它。

现在我在想,也许还有另外一个选择......在文档不是图像的情况下,只需使用小的UIWebView而不是图像,然后加载文档。它应该在细胞上看起来很小。只需确保将scalesPageToFit设置为YES。

修改

我很快就尝试了我的最后一个建议,但它确实奏效了。我创建了一个带有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;
}

它从互联网上加载随机PDF并将其显示为缩略图:

enter image description here