- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在寻找从 MS doc/xls 文档页面创建图像缩略图,但我一无所获。
对于 pdf 文档,我使用了 Quarz 框架,但在这种情况下我不能。
有帮助吗?
最佳答案
Web View 可用于制作 MS 文档预览。
我曾尝试用这段代码来做到这一点。它有效……但是…… WebView 需要在图形线程中工作,所以当这个操作运行时你的界面会变慢。也许你能优化一下。
标题
@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
关于iphone - 是否可以从 .doc 或 .xls 文档中获取缩略图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8475125/
我编写了一个 c# 程序,并在未安装 MS-Office 的 PC 中将其与文件扩展名(如 DOC)相关联。然后,我双击名称中包含空白字符的任何文件,我的程序将启动以打开该文件。我使用了以下语句: s
我试过创建、批量更新、从 https://developers.google.com/docs/api/how-tos/overview 获取. 即使在 batchUpdate 中,我也看不到编辑 t
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
我正在尝试使用新 API 更新 Google 文档中的表格。表格链接自 Google 表格。 我尝试了谷歌云中的 API 资源管理器。我能够以 json 格式提取文档,然后过滤出表格。但是在表 jso
将 Google Docs Java API 与 Google Apps 帐户一起使用,是否可以模拟用户并下载文件? 当我运行下面的程序时,它显然是登录到域并冒充用户,因为它检索其中一个文件的详细信息
我试图通过 apidoc 生成 API 文档 如果我的回应是一个数组 [ {"id" : 1, "name" : "John"}, {"id" : 2, "name" : "Mary"}
是否可以在没有身份验证的情况下在 Google Docs 中查询公开共享的用户文档? 我正在寻找的特定最终目标是能够提供用户 ID,然后列出所有公开共享的文档,并在集合中带有特定标记。 谢谢。 最佳答
我对Elasticsearch映射感到困惑 首先,我创建了一个带有映射请求的文档 PUT /person { "mappings":{ "properties":{ "firs
我有一个可在一个电子表格中运行的 Google 文档查询。但是,当我复制电子表格时,查询不起作用,并且收到解析错误:无法解析函数 QUERY 参数 2 的查询字符串:NO_COLUMNCol2。 我的
我有一个如下所示的 XML 文档: _1 _2 TASK _3 TASK 我必须使用第一个文档中的节点属性创建另一
我没有看到什么? RTD features页面说: PDF Generation When you build your project on RTD, we automatically build
我有一个网页,我在 iFrame 中嵌入了一个 Google 文档查看器 (其中 URL-encoded-URL 是实际编码的 URL)。 对于我的许多/大多数用户,Google PDF 文档查看器
我如何在我的项目中使用 GOOGLE DOCS,我正在使用 asp.net 和 C# 作为后面的代码。 基本上我需要在浏览器中以只读形式显示一些 pdf、doc、dox、excel 文档。 提前致谢
从看起来像的 Google Doc 开始: * Item 我希望进行一系列 API 调用以将文档转换为: * Item - Subitem 但是,我不知道如何使用 API 做到这一点。 Crea
我需要控制我网站中嵌入的 Google 文档查看器。更具体地说,我需要能够启用/禁用 Google 幻灯片 View 的控件,并能够使用 JavaScript 启动/停止演示文稿。 我无法为此找到任何
我想使用 Google Docs API 将页眉和页脚添加到现有的 Google 文档文件中. 看着documents.batchUpdate ( link ) 我们可以插入文本、替换文本、添加图像和
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 已关闭 4 年前。 Improve
我已按照 GitHub 的文档进行操作,并使用 docs 成功发布了我的项目页面。我的项目存储库下的文件夹。但我想知道如何解决这个小问题: 我正在开发一个 JavaScript 库 wesa.js ,
我的程序正在创建文档,每个文档都有需要放入其中的文本。任何调用 InsertTextRequest 的尝试调用错误。 List requests = new ArrayList<>(); reques
基于此: Set field to automatically insert time-stamp on UPDATE? 我正在尝试创建适合我需要的触发器,但我发现使用 OLD 和 NEW 关键字不方
我是一名优秀的程序员,十分优秀!