【问题标题】:Is there an easy way to create thumbnails of various document formats like Word, Excel on iOS?是否有一种简单的方法可以在 iOS 上创建各种文档格式(如 Word、Excel)的缩略图?
【发布时间】:2026-01-24 12:35:01
【问题描述】:

iOS SDK 中是否有内置机制可用于呈现任意文档类型的 缩略图 (UIImage),例如 Word、Excel、PDF、图像格式 等 - 任何 iOS 都可以预览? 它只需要创建第一页的缩略图。

我有代码可以缩小 PDF 和图像,但我想,因为 iOS 可以预览 Word 和其他,也许有内置的东西?

【问题讨论】:

  • 我认为您正在寻找 QuickLook 框架:developer.apple.com/library/ios/#DOCUMENTATION/FileManagement/…。不过我不太熟悉它的用途。
  • 这不是我想要的。 QuickLook 框架加载整个文件并进行预览。我只需要第一页。我也许可以预览它,然后在图像的上下文中渲染,但这只是矫枉过正。

标签: ios


【解决方案1】:

使用技巧打开office文档文件(docx、xls、pptx),在缩略图栏中显示所有页面,让用户在点击任何缩略图时在同一视图中打开任何页面。

步骤如下

  • 在 UIWebView 中打开文件
  • 截取每个页面并添加可滚动的缩略图视图栏(递归)
  • 在每个缩略图上添加一个自定义按钮以启用用户交互

导入 QuartzCore.framework

类似 Configure.h 的文件

#import <UIKit/UIKit.h>
#include <QuartzCore/QuartzCore.h>

@interface ViewController : UIViewController<UIWebViewDelegate>
{
    BOOL finished;
    float currentOffset;
    float pointToAddNextThumbnail;
    NSURLRequest *request;
    int totalFileHeight;
    int currentExecutingSnapshot;
}
@property(nonatomic,retain) UIWebView* webView;
@property(nonatomic,retain) UIScrollView* thumbnailScrollView;
@end

.m 文件喜欢

#import "ViewController.h"

@interface ViewController ()

@end
#define pageHeight 360
#define pageWidth 320
#define thumbnailHeight 88
#define thumbnailWidht 70
@implementation ViewController
@synthesize webView,thumbnailScrollView;
#pragma mark
#pragma mark VC Delegates
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Reader" ofType:@"doc"];
    NSURL *url = [NSURL fileURLWithPath:path];
    request = [NSURLRequest requestWithURL:url];

    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, pageWidth, pageHeight)];

    [self.webView  setScalesPageToFit:YES];
    self.webView.delegate = self;
    [self.webView  loadRequest:request];
    [self.view addSubview:self.webView ];

    self.thumbnailScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, pageHeight+5, pageWidth, thumbnailHeight)];
    [self.thumbnailScrollView setBackgroundColor:[UIColor grayColor]];
    [self.view addSubview:self.thumbnailScrollView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark
#pragma mark webView Delegate Methods
- (void)webViewDidFinishLoad:(UIWebView *)webView {
   //  NSLog(@"webViewDidFinishLoad");
    CGSize z =  self.webView.scrollView.contentSize;
    totalFileHeight= z.height;
    NSLog(@"totalFileHeight in pxls %d",totalFileHeight);
    totalFileHeight=totalFileHeight/pageHeight;
    NSLog(@"totalFileHeight in pages %d",totalFileHeight);
    [self takeScreenShotAndReloadWebViewWithPage];
}

#pragma mark
#pragma mark utitlityMehtods
-(void)takeScreenShotAndReloadWebViewWithPage
{
    float widthOfThumbnailForScrollView = (thumbnailWidht+5);
    float widhtOfScrollViewContant = (currentExecutingSnapshot*widthOfThumbnailForScrollView)+widthOfThumbnailForScrollView;
    self.thumbnailScrollView.contentSize = CGSizeMake(widhtOfScrollViewContant,50);
    self.webView.scrollView.contentOffset = CGPointMake(0, currentOffset);
   // [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"window.scrollTo(0,%f)",currentOffset]];

    //take Snapshot
    UIGraphicsBeginImageContext(CGSizeMake(320, pageHeight));
    [self.webView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    //set frames of thumbailButton and thumnailImage
    CGRect thumbnailFrame = CGRectMake(pointToAddNextThumbnail, 0, thumbnailWidht, thumbnailHeight);
    UIButton *thumbnailButton = [[UIButton alloc] initWithFrame:thumbnailFrame];
    UIImageView *thumbnailImage =[[UIImageView alloc] initWithFrame:thumbnailFrame];
    [thumbnailImage setImage:img];
    thumbnailButton.tag = currentOffset;
    [thumbnailButton setBackgroundColor:[UIColor clearColor]];
    [thumbnailButton addTarget:self action:@selector(thumnailButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.thumbnailScrollView addSubview:thumbnailImage];
    [self.thumbnailScrollView addSubview:thumbnailButton];
    [thumbnailImage release];
    [thumbnailButton release];

    pointToAddNextThumbnail = pointToAddNextThumbnail+thumbnailWidht+5;
    currentOffset = currentOffset + pageHeight;

    if (currentExecutingSnapshot < totalFileHeight)
    {
        NSLog(@"currentExecutingSnapshot %d",currentExecutingSnapshot);
        currentExecutingSnapshot ++;
        //[self.webView  loadRequest:request];
        //make a recursive call and take snapshot of all pages
        [self takeScreenShotAndReloadWebViewWithPage];
    }
    else
    {
        [self finishedFethingThumbnails];
    }
}
-(void)finishedFethingThumbnails
{
     self.webView.scrollView.contentOffset = CGPointMake(0, 0);
}

#pragma mark
#pragma mark IBaction
-(IBAction)thumnailButtonClicked:(UIButton*)sender
{
    [[self.webView scrollView] setZoomScale:0 animated:YES];
    [[self.webView scrollView] setContentOffset:CGPointMake(0, sender.tag) animated:YES];
}

#pragma mark
#pragma mark dealloc
-(void)dealloc
{
    [webView release];
    [thumbnailScrollView release];
    [super dealloc];
}

@end

http://abdus.me/downloads/testThumbnail.zip下载示例项目

【讨论】:

    最近更新 更多