【问题标题】:Is it possibile get thumbail image from .doc or .xls document?是否可以从 .doc 或 .xls 文档中获取缩略图?
【发布时间】:2012-01-18 11:52:25
【问题描述】:

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

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

一些帮助?

【问题讨论】:

    标签: iphone objective-c ios thumbnails doc


    【解决方案1】:

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

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

    标题

    @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
    

    【讨论】:

      猜你喜欢
      • 2012-05-18
      • 2011-12-04
      • 2013-07-05
      • 1970-01-01
      • 1970-01-01
      • 2010-09-18
      • 1970-01-01
      • 2012-01-08
      • 2018-01-13
      相关资源
      最近更新 更多