【问题标题】:How to create thumbnail image for document(doc, html) loaded in UIWebView in iOS如何为 iOS 的 UIWebView 中加载的文档(doc、html)创建缩略图
【发布时间】:2014-06-01 05:18:03
【问题描述】:
  • tableviewam 中列出以下文档文件jpg、png、html、txt、doc、xls
  • 我得到了 jpg、png jpeg 等图像文件的缩略图...
  • tableviewdidselect 方法中导航到另一个视图控制器以在UIWebView 中加载这些文档文件。
  • 谁能告诉如何创建文档的缩略图以添加到tableview 中的单元格图像。

1.Tableview中的第一个ViewController

- (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];
}

输出:

【问题讨论】:

  • 您想要显示文档某些内容的实际缩略图?或者也许只使用静态图像就可以了......
  • @MatíasR:只需要实际的缩略图。
  • 您可以做的一件事是在 UIWebView 中加载文档并截取屏幕截图。将其存储为图像,然后将其用作缩略图。我会将代码作为答案发布,它作为评论将无法阅读。
  • 您是如何获得 pdf 的缩略图的?我的服务器中存储了 pdf,想要显示缩略图,但没有发现任何有用的东西。

标签: ios objective-c uiwebview thumbnails


【解决方案1】:

您可以在 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;
}

它从 Internet 加载随机 PDF 并将其显示为缩略图:

【讨论】:

  • 我不明白。您应该能够像使用 PDF 一样在 UIWebView 中加载 HTML 和 DOC。
  • 为了获取pdf的缩略图,我使用了核心图形方法..但没有找到从webview获取文档缩略图的解决方案
  • 我明白了。我相信无论如何它应该可以工作,但如果没有,您可以尝试这种方法:code.tutsplus.com/tutorials/…
猜你喜欢
  • 2011-12-04
  • 2016-09-13
  • 1970-01-01
  • 2011-04-13
  • 2013-09-30
  • 2011-11-28
  • 1970-01-01
  • 2012-01-29
  • 2014-09-12
相关资源
最近更新 更多