是否可以从.doc或.xls文档中获取缩略图图像? [英] Is it possibile get thumbail image from .doc or .xls document?

查看:108
本文介绍了是否可以从.doc或.xls文档中获取缩略图图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找从MS doc / xls文档页面创建一个图像缩略图,
但我一无所知。

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

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

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

有些帮助吗?

推荐答案

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

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

我试过用这段代码做过一次。
它的工作原理......但是...... Web视图需要在图形线程中工作,所以当这个操作运行时你的界面会变慢。也许你可以优化它。

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.

标题

@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

这篇关于是否可以从.doc或.xls文档中获取缩略图图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆