是否可以从 .doc 或 .xls 文档获取缩略图?

发布于 2024-12-21 06:22:36 字数 105 浏览 1 评论 0原文

我正在寻找从 MS doc/xls 文档页面创建图像缩略图, 但我什么也没找到。

对于 pdf 文档,我使用了 Quarz 框架,但在这种情况下我不能。

有帮助吗?

I'm looking for create an image thumbnail from a MS doc/xls document's page,
but I found nothing about it.

For pdf documents I used Quarz framework, but I can't in this case.

Some help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

护你周全 2024-12-28 06:22:36

Web 视图可用于制作 MS 文档预览。

我曾经尝试过用这段代码来做到这一点。
它可以工作...但是...网络视图需要在图形线程中工作,因此当此操作运行时,您的界面会变慢。也许你可以优化一下。

标题

@interface WebViewThumbnailGenerationOperation: NSOperation <UIWebViewDelegate> {
    BOOL finished;
}
@property(nonatomic,retain) NSURL* documentURL;
@property(nonatomic,retain) UIWebView* webView;

-(void)saveThumbnail:(UIImage*)thumbnail;

@end

代码

/**************************************************************************************************/
#pragma mark - WebViewBased Thumbnails

@implementation WebViewThumbnailGenerationOperation

@synthesize documentURL,webView;

-(void)dealloc {
    RELEASE_SAFELY(documentURL);
    [super dealloc];
}


- (void)loadWebView {
    if (self.isCancelled) {
        return;
    }
    self.webView = [[[UIWebView alloc] init] autorelease];
    self.webView.delegate = self;
    self.webView.scalesPageToFit = YES;
    self.webView.frame = CGRectMake(0, 0, 290, 290);
    NSURLRequest *request = [NSURLRequest requestWithURL:documentURL];
    [self.webView loadRequest:request];    
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    if (self.isCancelled) {
        return;
    }
    UIGraphicsBeginImageContext(CGSizeMake(290,290));
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self.webView.layer renderInContext:context];
    UIImage *thumbnail = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    [self performSelectorInBackground:@selector(saveThumbnail:) withObject:thumbnail];

    self.webView = nil;
}

-(void)saveThumbnail:(UIImage*)thumbnail {
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    if (self.isCancelled) {
        return;
    }

    if(!thumbnail) {
        return;
    }

    NSData* thumbnailData = UIImageJPEGRepresentation(thumbnail,0.8);
    [IOHelper saveThumbnailData:thumbnailData forDocumentURL:documentURL];

    [self willChangeValueForKey:@"isFinished"];
    finished = YES;
    [self didChangeValueForKey:@"isFinished"];

    [pool release];
}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    NSLog(@"Preview failed for %@ error %@",document.name,error);
    [self willChangeValueForKey:@"isFinished"];
    finished = YES;
    [self didChangeValueForKey:@"isFinished"];
    self.webView = nil;
}

-(void)start {
    finished = NO;
    [super start];
}

- (void)main {
    if (self.isCancelled) {
        return;
    }
    [self performSelectorOnMainThread:@selector(loadWebView) withObject:nil waitUntilDone:YES];
}

-(BOOL)isFinished {
    return finished;
}

@end

编辑: ARC 版本!

标头

@interface WebViewThumbnailGenerationOperation: NSOperation <UIWebViewDelegate>

@property(nonatomic, strong) NSURL* documentURL;
@property(nonatomic, strong) UIWebView* webView;
@property(nonatomic) BOOL finished;


-(void)saveThumbnail:(UIImage*)thumbnail;

@end

代码

/**************************************************************************************************/
#pragma mark - WebViewBased Thumbnails

@implementation WebViewThumbnailGenerationOperation

- (void)loadWebView {
    if (self.isCancelled) {
        return;
    }
    self.webView = [[UIWebView alloc] init];
    self.webView.delegate = self;
    self.webView.scalesPageToFit = YES;
    self.webView.frame = CGRectMake(0, 0, 290, 290);
    NSURLRequest *request = [NSURLRequest requestWithURL:documentURL];
    [self.webView loadRequest:request];    
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    if (self.isCancelled) {
        return;
    }
    UIGraphicsBeginImageContext(CGSizeMake(290,290));
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self.webView.layer renderInContext:context];
    UIImage *thumbnail = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    [self performSelectorInBackground:@selector(saveThumbnail:) withObject:thumbnail];

    self.webView = nil;
}

-(void)saveThumbnail:(UIImage*)thumbnail {
    @autoreleasepool {
        if (self.isCancelled) {
            return;
        }

        if(!thumbnail) {
            return;
        }

        NSData* thumbnailData = UIImageJPEGRepresentation(thumbnail,0.8);
        [IOHelper saveThumbnailData:thumbnailData forDocumentURL:documentURL];

        [self willChangeValueForKey:@"isFinished"];
        finished = YES;
        [self didChangeValueForKey:@"isFinished"];

    }
}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    NSLog(@"Preview failed for %@ error %@", document.name, error);
    [self willChangeValueForKey:@"isFinished"];
    finished = YES;
    [self didChangeValueForKey:@"isFinished"];
    self.webView = nil;
}

-(void)start {
    finished = NO;
    [super start];
}

- (void)main {
    if (self.isCancelled) {
        return;
    }
    [self performSelectorOnMainThread:@selector(loadWebView) withObject:nil waitUntilDone:YES];
}

-(BOOL)isFinished {
    return finished;
}

@end

A web view can be used for making a MS doc preview.

I've tried once to do that with this piece of code.
It works ... but ... the web view need to work in graphical thread, so when this operation is running your interface is slower. Maybe can you optimized that.

Header

@interface WebViewThumbnailGenerationOperation: NSOperation <UIWebViewDelegate> {
    BOOL finished;
}
@property(nonatomic,retain) NSURL* documentURL;
@property(nonatomic,retain) UIWebView* webView;

-(void)saveThumbnail:(UIImage*)thumbnail;

@end

Code

/**************************************************************************************************/
#pragma mark - WebViewBased Thumbnails

@implementation WebViewThumbnailGenerationOperation

@synthesize documentURL,webView;

-(void)dealloc {
    RELEASE_SAFELY(documentURL);
    [super dealloc];
}


- (void)loadWebView {
    if (self.isCancelled) {
        return;
    }
    self.webView = [[[UIWebView alloc] init] autorelease];
    self.webView.delegate = self;
    self.webView.scalesPageToFit = YES;
    self.webView.frame = CGRectMake(0, 0, 290, 290);
    NSURLRequest *request = [NSURLRequest requestWithURL:documentURL];
    [self.webView loadRequest:request];    
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    if (self.isCancelled) {
        return;
    }
    UIGraphicsBeginImageContext(CGSizeMake(290,290));
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self.webView.layer renderInContext:context];
    UIImage *thumbnail = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    [self performSelectorInBackground:@selector(saveThumbnail:) withObject:thumbnail];

    self.webView = nil;
}

-(void)saveThumbnail:(UIImage*)thumbnail {
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    if (self.isCancelled) {
        return;
    }

    if(!thumbnail) {
        return;
    }

    NSData* thumbnailData = UIImageJPEGRepresentation(thumbnail,0.8);
    [IOHelper saveThumbnailData:thumbnailData forDocumentURL:documentURL];

    [self willChangeValueForKey:@"isFinished"];
    finished = YES;
    [self didChangeValueForKey:@"isFinished"];

    [pool release];
}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    NSLog(@"Preview failed for %@ error %@",document.name,error);
    [self willChangeValueForKey:@"isFinished"];
    finished = YES;
    [self didChangeValueForKey:@"isFinished"];
    self.webView = nil;
}

-(void)start {
    finished = NO;
    [super start];
}

- (void)main {
    if (self.isCancelled) {
        return;
    }
    [self performSelectorOnMainThread:@selector(loadWebView) withObject:nil waitUntilDone:YES];
}

-(BOOL)isFinished {
    return finished;
}

@end

Edit: ARC version!

Header

@interface WebViewThumbnailGenerationOperation: NSOperation <UIWebViewDelegate>

@property(nonatomic, strong) NSURL* documentURL;
@property(nonatomic, strong) UIWebView* webView;
@property(nonatomic) BOOL finished;


-(void)saveThumbnail:(UIImage*)thumbnail;

@end

Code

/**************************************************************************************************/
#pragma mark - WebViewBased Thumbnails

@implementation WebViewThumbnailGenerationOperation

- (void)loadWebView {
    if (self.isCancelled) {
        return;
    }
    self.webView = [[UIWebView alloc] init];
    self.webView.delegate = self;
    self.webView.scalesPageToFit = YES;
    self.webView.frame = CGRectMake(0, 0, 290, 290);
    NSURLRequest *request = [NSURLRequest requestWithURL:documentURL];
    [self.webView loadRequest:request];    
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    if (self.isCancelled) {
        return;
    }
    UIGraphicsBeginImageContext(CGSizeMake(290,290));
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self.webView.layer renderInContext:context];
    UIImage *thumbnail = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    [self performSelectorInBackground:@selector(saveThumbnail:) withObject:thumbnail];

    self.webView = nil;
}

-(void)saveThumbnail:(UIImage*)thumbnail {
    @autoreleasepool {
        if (self.isCancelled) {
            return;
        }

        if(!thumbnail) {
            return;
        }

        NSData* thumbnailData = UIImageJPEGRepresentation(thumbnail,0.8);
        [IOHelper saveThumbnailData:thumbnailData forDocumentURL:documentURL];

        [self willChangeValueForKey:@"isFinished"];
        finished = YES;
        [self didChangeValueForKey:@"isFinished"];

    }
}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    NSLog(@"Preview failed for %@ error %@", document.name, error);
    [self willChangeValueForKey:@"isFinished"];
    finished = YES;
    [self didChangeValueForKey:@"isFinished"];
    self.webView = nil;
}

-(void)start {
    finished = NO;
    [super start];
}

- (void)main {
    if (self.isCancelled) {
        return;
    }
    [self performSelectorOnMainThread:@selector(loadWebView) withObject:nil waitUntilDone:YES];
}

-(BOOL)isFinished {
    return finished;
}

@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文