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

标签 ios objective-c uiwebview thumbnails

  • tableview我列出了以下文档文件 jpg、png、html、txt、doc、xls .
  • 我得到了 jpg、png jpeg 等图像文件的缩略图...
  • 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];
    }
    

    输出:

    enter image description here

    最佳答案

    您可以在 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 并将其显示为缩略图:

    enter image description here

    关于ios - 如何为 iOS 的 UIWebView 中加载的文档(doc、html)创建缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23124603/

    相关文章:

    ios - 以编程方式在蜂窝和 WiFi 之间切换?

    java - 检查是否安装了 3rd 方应用程序?

    ios - Interface builder 和 .m@.h 之间的冲突 : who win?

    objective-c - 在 iOS 中计算非整数指数

    ios - 表格单元格内的 UIImage 错误位置

    ios - 如何在 WKWebView 上的指定位置获取消息?

    objective-c - iOS 水平滚动日历

    objective-c - 非字母数字字符可以用作选择器吗?

    iphone - 在 native 代码与 UIWebView 不同的电话调用后返回应用程序行为

    iOS UIWebView 通过附加字符串设置 UserAgent 不起作用