Is it possibile get thumbail image from .doc or .xls document?

﹥>﹥吖頭↗ 提交于 2019-12-21 06:17:32

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/8475125/is-it-possibile-get-thumbail-image-from-doc-or-xls-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!