点击QLPreviewController ios 4.2中的菜单操作按钮,iPhone-应用程序崩溃

Kap*_*man 11 iphone

 When I try to click the menu action button in QLPreviewController the application crashes.
Run Code Online (Sandbox Code Playgroud)

这就是我在委托方法中所做的

- (id)previewController:(QLPreviewController *)previewController previewItemAtIndex:(NSInteger)index
{

    NSMutableString*Url = [[NSMutableString alloc] initWithFormat:@"http://10.30.24.21/Documents/abc.doc];

    NSURL *fileURL;
    fileURL = [NSURL URLWithString:Url];// the url of the file which is present in NAS device
    [Url release];
    return fileURL;
}
Run Code Online (Sandbox Code Playgroud)

这是崩溃报告

2011-01-11 12:21:36.717 iLink[5548:207] *** Assertion failure in -[UIDocumentInteractionController setURL:], /SourceCache/UIKit_Sim/UIKit-1447.6.4/UIDocumentInteractionController.m:1060
2011-01-11 12:21:36.720 iLink[5548:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UIDocumentInteractionController: invalid scheme https.  Only the file scheme is supported.'
Run Code Online (Sandbox Code Playgroud)

当我尝试在本地预览文件时,菜单操作按钮单击不会崩溃.

那里我会用

 NSURL *fileURL;
    fileURL = [NSURL fileURLWithPath:filePath];// filePath is local file path.
Run Code Online (Sandbox Code Playgroud)

我明白当我们预览本地文件时([NSURL fileURLWithPath:filePath])菜单操作按钮点击不会崩溃,当我们从服务器预览文件时([NSURL URLWithString:url])菜单操作按钮点击崩溃.

我有两个问题,1.我们可以禁用菜单操作按钮吗?2.有没有办法使用[NSURL URLWithString:Url]来避免崩溃?

谢谢

小智 36

需要使用

[NSURL fileURLWithPath:urPath]
Run Code Online (Sandbox Code Playgroud)


Tim*_*del 9

虽然以前的答案给出了正确的技术响应,但我想详细说明.

QLPreviewItem的API文档说URL 必须是文件类型的URL,NSURL文档说这意味着"使用该file:方案".

您还可以从Document Interaction Progamming指南中阅读更多内容,该指南提到QuickLook应该通过让您决定它的呈现方式来为您提供比UIDocumentInteractionController更多的控制,但它带来了与您已经相同的假设在本地获得一个文件,您只需要一种方法来显示它,并(使用QuickLook)使用AirPrint打印它.

在您的情况下,最好将文件下载到应用程序的Caches目录,然后打开QL预览 - 无论如何它已经被预览视图下载,因此您也可以抓住它以便也可以打印它.

NSArray *paths  = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
// Here you'll _probably_ want to use a filename that's got the correct extension...but YMMV
NSURL *cacheURL = [NSURL fileURLWithPath:[[paths objectAtIndex:0] stringByAppendingPathComponent: @"current.pdf"]];
Run Code Online (Sandbox Code Playgroud)

现在,获取原始URL并下载内容并保存cacheURL.如果您使用ASIHTTPRequest,它看起来像这样:

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:originalURL];
[request setDownloadDestinationPath:[cacheURL path]];
// Yup. You could optimize here if needed...
[request startSynchronous];
Run Code Online (Sandbox Code Playgroud)

然后在QuickLook视图中使用文件URL ...

- (id)previewController:(QLPreviewController *)previewController previewItemAtIndex:(NSInteger)index {
    // Assuming you've saved this somewhere, etc...
    return self.cacheURL;
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,您从NAS查看的每个新PDF都会覆盖前一个(相同名称,相同目录),因此您可以限制磁盘使用量.显然,有很多方法可以解决这个问题,所以选择一个适合你的方法.但关键是要在本地下载文件并至少保留它,直到QL视图被解除为止.


Mat*_*uch 2

有什么办法可以避免使用 [NSURL URLWithString:Url] 崩溃?

首先将文件下载到本地文件系统。