【问题标题】:CGPDFDocumentCreateWithURL will download the file in to documents directory or notCGPDFDocumentCreateWithURL 是否将文件下载到文档目录
【发布时间】:2011-05-04 11:00:17
【问题描述】:

借助将文件 url 传递给 CGPDFDocumentCreateWithURL,我正在使用我的 iPhone 访问我的 PHP 服务器中可用的 PDF 文件。每当我访问将下载到我的 iphone 文档目录的文件时,我的问题?

因为如果我的应用程序处于活动状态一段时间,我会收到错误 CFURLCreateDataAndPropertiesFromResource 错误代码 -14。收到此错误后,我的应用程序没有崩溃,但无法正常运行。

我无法理解内部流程?请帮我解决这个问题?

【问题讨论】:

    标签: iphone pdf cgpdfdocument


    【解决方案1】:

    我认为您误解了 CGPDFDocumentCreateWithURL 的作用。它不会将 PDF 文档下载到文件系统。它根据提供的 URL 在内存中创建 PDF。它不会将任何内容保存到文件系统中。

    强烈建议您使用网络类(NSURLConnection、ASI 等)首先下载 PDF,然后打开它。这将使您能够提供更好的反馈,并支持更高级的下载功能,例如恢复部分下载、提供进度和更好的线程管理。

    【讨论】:

    • 谢谢 :) 但是如何下载并保存 pdf 文件以供第一次使用,并将保存的文档用于以后使用
    【解决方案2】:

    保存 PDF

    NSString *urlString = @"http://www.web.com/url/for/document.pdf";
    
    NSURL *url = [NSURL URLWithString:urlString];
    
    NSData *data = [NSData dataWithContentsOfURL:url];
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    NSString *documentsDirectory = [paths objectAtIndex:0];
    
    NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:@"myLocalFileName.pdf"];
    
    [data writeToFile:pdfPath atomically:YES];
    

    然后将该保存的文件加载到网络视图中:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    NSString *documentsDirectory = [paths objectAtIndex:0];
    
    NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:@"myLocalFileName.pdf"];
    
    NSURL *url = [NSURL fileURLWithPath: pdfPath];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    [[self mainWebView] loadRequest:request];
    

    【讨论】:

    • 这不是一个好方法,或者至少你应该在主线程之外运行它。 dataWithContentsOfURL 是同步方式,不适合联网。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-02
    • 1970-01-01
    相关资源
    最近更新 更多