2
我在尋找從MS doc/xls文檔的頁面創建圖像縮略圖, ,但我沒有發現任何關於它的信息。是否可能從.doc或.xls文檔獲取縮略圖?
對於pdf文件,我使用了Quarz框架,但我不能在這種情況下。
有幫助嗎?
我在尋找從MS doc/xls文檔的頁面創建圖像縮略圖, ,但我沒有發現任何關於它的信息。是否可能從.doc或.xls文檔獲取縮略圖?
對於pdf文件,我使用了Quarz框架,但我不能在這種情況下。
有幫助嗎?
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版!
部首
代碼
/**************************************************************************************************/
#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