如何让 QuickLook 显示脱机文件?
我们的 iPad 应用程序可以显示文档并在需要时离线保存。 我有一个名为 DocumentViewController(从现在起名为 DVC)的 QLPreviewController 子类来显示它们。
应用程序的工作流程: - 用户单击文档名称,然后按下 DVC 以显示该文档。 - DVC 离线下载文件并在完成后显示。 (因此HTTP URL被下载,离线存储,并返回一个离线URL)
奇怪的是,只有PDF文件可以使用离线URL,其余的崩溃..(尽管它可以使用在线链接)
我做了一些测试,当我将 file:// 放在脱机链接之前时,应用程序不会崩溃,但 DVC 会向我提供有关该文件的一些信息(例如它是 excel 97-2004 文档)。
所以一些信息被转移,但我无法弄清楚问题是什么。
这是一些屏幕截图,然后是一些代码。
代码: 请注意,Document 是一个模型类,具有 id、名称、文件类型和 url 等文档属性。
//DVC QLPreviewController dataSource method for returning url
- (id <QLPreviewItem>) previewController: (QLPreviewController *) controller previewItemAtIndex: (NSInteger)index
{
[SaveHelper saveDocumentFileAndPropertyWithDocument:document];
//[SaveHelper getDocumentFileWithDocument:document]; without file://
//if I return document.documentUrl it is working with all files except iworks files)
return [SaveHelper getDocumentFileAsPathWithDocument:document]; //with file://
}
//SaveHelper methods
+ (NSString *)documentFilePathWithDocument:(Document *)document
{
return [[self documentFilePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%d.%@", DOCUMENT_FILE_PREFIX, document.documentId, document.documentType]];
}
+ (NSURL *)saveDocumentFileAndPropertyWithDocument:(Document *)document
{
if([self saveDocumentPropertyWithDocument:document])
{
return [self saveDocumentFileWithDocument:document];
}
return nil;
}
+ (NSURL *)saveDocumentFileWithDocument:(Document *)document
{
NSData *data = [NSData dataWithContentsOfURL:document.documentURL];
NSString *fullPath = [self documentFilePathWithDocument:document];
if([[NSKeyedArchiver archivedDataWithRootObject:data] writeToFile:fullPath atomically:YES])
{
return [NSURL fileURLWithPath:fullPath];
}
return nil;
}
+ (NSURL *)getDocumentFileWithDocument:(Document *)document
{
return [NSURL fileURLWithPath:[self documentFilePathWithDocument:document]];
}
+ (NSURL *)getDocumentFileAsPathWithDocument:(Document *)document
{
return [NSURL fileURLWithPath:[@"file://" stringByAppendingPathComponent:[[self getDocumentFileWithDocument:document] absoluteString]]];
}
如果需要更多代码,请直接说明。
编辑:
记录通过“getDocumentFileAsPathWithDocument”方法传递的 URL 时:
url: file:/var/mobile/Applications/xx-xx/Documents/documentFiles/file_20.pdf
url: file:/var/mobile/Applications/xx-xx/Documents/documentFiles/file_80.docx
PDF 文件正在工作而 docx 不工作的地方
当我尝试从本地存储加载图像(jpg)时,出现黑屏并显示以下错误消息:
warning: Unable to read symbols for /Developer/Platforms/iPhoneOS.platform/DeviceSupport/4.3.5 (8L1)/Symbols/System/Library/Frameworks/QuickLook.framework/DisplayBundles/Image.qldisplay/Image (file not found).
warning: No copy of Image.qldisplay/Image found locally, reading from memory on remote device. This may slow down the debug session.
:
编辑 webview 也不适用于本地 url。 PDF 没问题,但 Office 文件显示一条消息“无法读取文档,文件格式无效”。 iWorks 文档给出与快速查看相同的错误。我认为它在保存和加载格式的某个地方,我将它们保存为 NSDATA 但之后 iPad 没有任何提示来查看它是否是一个 word 文档(只有扩展名)。
Our iPad app can show Documents and save them offline when needed.
I've got a QLPreviewController subclass named DocumentViewController (named DVC from now on) for showing them.
Workflow of the app:
- The user clicks a name of a document and the DVC is pushed on to show the document.
- The DVC downloads the file offline and shows it when done.
(So the HTTP URL is downloaded, stored offline, and an offline URL is returned)
The weird thing is, is that only PDF files are working with the offline URL, and the rest crashes.. (it works with online links though)
I did some tests and when I put file:// before the offline link the app does not crash but the DVC is ging me some information about the file (like that it is a excel 97-2004 document).
So some info is transferred, but I can't figure out what the problem is.
Here are some screenshots and after that some code.
code:
Note that Document is a model class with document properties like id, name, file type and url.
//DVC QLPreviewController dataSource method for returning url
- (id <QLPreviewItem>) previewController: (QLPreviewController *) controller previewItemAtIndex: (NSInteger)index
{
[SaveHelper saveDocumentFileAndPropertyWithDocument:document];
//[SaveHelper getDocumentFileWithDocument:document]; without file://
//if I return document.documentUrl it is working with all files except iworks files)
return [SaveHelper getDocumentFileAsPathWithDocument:document]; //with file://
}
//SaveHelper methods
+ (NSString *)documentFilePathWithDocument:(Document *)document
{
return [[self documentFilePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%d.%@", DOCUMENT_FILE_PREFIX, document.documentId, document.documentType]];
}
+ (NSURL *)saveDocumentFileAndPropertyWithDocument:(Document *)document
{
if([self saveDocumentPropertyWithDocument:document])
{
return [self saveDocumentFileWithDocument:document];
}
return nil;
}
+ (NSURL *)saveDocumentFileWithDocument:(Document *)document
{
NSData *data = [NSData dataWithContentsOfURL:document.documentURL];
NSString *fullPath = [self documentFilePathWithDocument:document];
if([[NSKeyedArchiver archivedDataWithRootObject:data] writeToFile:fullPath atomically:YES])
{
return [NSURL fileURLWithPath:fullPath];
}
return nil;
}
+ (NSURL *)getDocumentFileWithDocument:(Document *)document
{
return [NSURL fileURLWithPath:[self documentFilePathWithDocument:document]];
}
+ (NSURL *)getDocumentFileAsPathWithDocument:(Document *)document
{
return [NSURL fileURLWithPath:[@"file://" stringByAppendingPathComponent:[[self getDocumentFileWithDocument:document] absoluteString]]];
}
If more code needed, just say.
EDIT:
When logging the URL passed trough the 'getDocumentFileAsPathWithDocument' method:
url: file:/var/mobile/Applications/xx-xx/Documents/documentFiles/file_20.pdf
url: file:/var/mobile/Applications/xx-xx/Documents/documentFiles/file_80.docx
Where the PDF file is working and the docx not
When I try to load an image(jpg) from local storage I get a black screen with this error message:
warning: Unable to read symbols for /Developer/Platforms/iPhoneOS.platform/DeviceSupport/4.3.5 (8L1)/Symbols/System/Library/Frameworks/QuickLook.framework/DisplayBundles/Image.qldisplay/Image (file not found).
warning: No copy of Image.qldisplay/Image found locally, reading from memory on remote device. This may slow down the debug session.
EDIT:
The webview does not work either with the local urls. PDF is fine but the office files gives an message "Unable to read Document, the file format is invalid". The iWorks documents give the same error as the quicklook. I think its somewhere at the save and load of the format, I savve them as a NSDATA but after that there is no hint for the iPad to see if it is for example a word document (only the extension).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您尚未发布下载代码,但我相信问题就在那里。 Pages 中的文件(扩展名为 .pages)不是实际文件,它们是捆绑包,即包含文件并在 Finder 中显示为单个项目的目录(在 Finder 中查找 .pages 文件,右-单击它并选择“显示内容”)。下载 .pages 文件实际上就像下载目录:它取决于 Web 服务器您得到什么样的结果,但它很可能是一个错误页面。
您可以检测到它是一个 .pages 文件并尝试手动下载其所有内容,但是您必须研究文件的结构以查看这是否可能,因为您不太可能从网络服务器。
.ppt 和 .xls 文件的结果对我来说看起来很正常;我认为 iPad 根本不可能预览 MS Office 文档。
编辑:抱歉,我刚刚读到 iOS可以预览 MS Office 文档。也许文档在下载过程中以某种方式损坏了?您是否尝试过将下载位置设置为应用程序的文档文件夹并启用 iTunes 文件共享?这样,您就可以下载一些文档,将它们从您的设备上拉下来,然后尝试在您的电脑上打开它,看看是否有效。
You haven't posted your download code, but I believe that the problem is there. Files from Pages (.pages extension) aren't actual files, they are bundles, i.e. directories that contain files and show as a single item in Finder (look up a .pages file in Finder, right-click it and select 'Show contents'). Downloading a .pages file is actually like downloading a directory: it depends on the web server what kind of result you get but it's most likely an error page.
You could detect that it's a .pages file and try to download all of its contents manually, but you'd have to study the structure of the files to see if that's possible because it's unlikely that you can request the contents of the directory from a web server.
The results for the .ppt and .xls files look normal to me; I think it unlikely that the iPad can preview MS Office documents at all.
Edit: apologies, I just read that iOS can preview MS Office documents. Perhaps the documents get somehow corrupted during download? Have you tried to set your download location to the app's documents folder and enable iTunes file sharing? That way you can download some documents, pull them off your device and then try to open it on your PC to see if that works.
我们终于找到了解决方案!
我是对的,问题出在保存文档上。
我需要更改保存方法:
所以使用文件管理器而不是使用 keyedarchiver 保存它。
We finally found the solution!
I was right that the problem was with saving the document.
I needed to change the save method in:
SO saving it with the filemanager and not with a keyedarchiver.
您是否检查过在线和离线文件的大小是否相同?可能是文件下载不完整
尝试使用带有普通 NSURL 对象的 MS Office 文档的 URL 并在 UIWebView 中打开。那么它可以工作吗(这样我们就知道它是文档还是您的类)?
尝试在 getDocumentFileAsPathWithDocument 中使用 NSURL 的 fileURLWithPath: 方法:返回的 URL 可能不正确(虽然从日志中看起来不像,但尝试一下也没什么坏处)
Did you check if the size of the files is the same both online and offline? It is possible that the file download wasn't complete
Try using the URL of the MS Office documents with a normal NSURL object and opening in a UIWebView. Does it work then (so we know if its the document or your class)?
Try using NSURL's fileURLWithPath: method in the getDocumentFileAsPathWithDocument: It is possible that the URL being returned is incorrect (though doesn't look like it from the logs but doesn't hurt to try)
首先,使用此代码确保您的文档在那里,因为我认为错误是由您的文档路径引起的。
first of all, use this code to make sure your documents are there,because i think the error cause by your documents path.
如果任何人遇到同样的问题,即使您执行了上述所有建议。
(当我从谷歌驱动器下载一些文件时,我遇到了同样的问题。)
试试这个!
将“x”放在文件扩展名末尾,以便将其识别为新版本的格式。
(它仅适用于“doc”和“ppt”文件,不适用于“xls”文件)
是的,我知道这不是解决这个问题的合适方法,但是
值得一试。
相信我,我尝试了一切!
希望你能帮忙。
If any of one have same problem even though you did everything suggestions above.
(I had same problem, when I downloaded some files from google drive.)
Try this!
Put 'x' end of your file extension to be recognized as a new version of format.
(it's working only for 'doc' and 'ppt' files, not for 'xls' files)
Yes, I know this is not a appropriate way to solve this problem, but
it's worth to try it.
Believe me I tried everything!
Hope you help.