1

我有一个应用程序,我在其中通过使用从捆绑包中加载 pdf

CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("hhhh.pdf"), NULL, NULL);.

现在我想从我的网络服务器加载它。我使用字符串 url 完成了它

NSString *strUlr = [NSString stringWithFormat:@"https://s3.amazonaws.com/hgjgj.pdf"];

CFStringRef aCFString = (CFStringRef)strUlr;

//CFURLRef pdfURL = aCFString;
// NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

//pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);

CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(),aCFString, NULL, NULL);
pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
   NSLog(@"%@",pdf);
//CFRelease(pdfURL);

但没有成功..任何人都知道如何使这段代码工作。由于某些原因,我不需要将它加载到pdf中。有人可以帮助我吗?

4

3 回答 3

1

您仍然尝试获取应该在应用程序包中的资源的 url,而不是在远程服务器上。创建 url 的正确方法是 CFURLCreateWithString:

CFURLRef pdfURL = CFURLCreateWithString(NULL, aCFString, NULL);

请记住,在显示之前下载您的 pdf 并将其缓存在本地可能会更好 - 这种方法还将为您提供更多的错误处理灵活性和可能的​​ url 身份验证挑战

于 2012-04-27T09:14:30.323 回答
0

如果我没记错的话,那么您可能想从 iPhone 中的网络服务器读取 pdf,如果是这样,那么您可以使用 UIWebView 来满足您的要求:这是您可以在本地读取和存储它的方法:

首先创建 UIWebView 并包含 << UIWebViewDelegate >>

  NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
  NSString *documentsPath = [paths objectAtIndex:0];
  NSString *filePath = [documentsPath stringByAppendingPathComponent:@"YourPDF.pdf"]; 
 if(![[NSFileManager defaultManager] fileExistsAtPath:filePath]){
           // download file , here "https://s3.amazonaws.com/hgjgj.pdf" = pdf downloading link
         NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"https://s3.amazonaws.com/hgjgj.pdf"]];
            //Store the downloaded file  in documents directory as a NSData format
         [pdfData writeToFile:filePath atomically:YES];
    }

     NSURL *url = [NSURL fileURLWithPath:filePath];
     NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
     [yourWebView setUserInteractionEnabled:YES];
     [yourWebView setDelegate:self];
     yourWebView.scalesPageToFit = YES;
     [yourWebView loadRequest:requestObj];
于 2012-09-21T05:06:07.680 回答
0

仅出于阅读目的,您可以通过 url 在 Uiwebview 中加载 pdf 文件。

于 2012-04-27T09:19:52.743 回答