【问题标题】:When we open pdf in iPhone then how to save this pdf in iphone当我们在 iPhone 中打开 pdf 时,如何在 iphone 中保存此 pdf
【发布时间】:2013-07-22 08:14:26
【问题描述】:

我对 iOS 很陌生。我创建PDF 并将此PDF 加载到UIWebView
现在这一次我想在 iPhone 中保存或下载这个PDF,当我们点击下载按钮然后所有退出PDF 支持者显示就像打开 ibook 一样,在 chrome 中打开。这种类型的选项会显示,但是当我们点击任何一个时,我的应用程序就会关闭。

  -(void)show_Button
  {

    NSArray *docDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   NSString *docDirectory = [docDirectories objectAtIndex:0];

   NSString *filePAth = [docDirectory stringByAppendingPathComponent:@"myPDF.pdf"];

   NSLog(@"filePath = %@", filePAth);
   NSURL *url2 = [NSURL fileURLWithPath:filePAth];
   NSLog(@"url2 = %@", url2);

   UIDocumentInteractionController *docContr = [UIDocumentInteractionController
                                             interactionControllerWithURL:url2];
   docContr.delegate=self;
  [docContr presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
  }

那么如何在 Iphone 中保存或下载这个 pdf 请解决这个问题....

【问题讨论】:

  • 您正在从目录中获取路径,并且文件已经存在。为什么要重新保存它?
  • 如何去那里我的意思是打开 pdf 实际上我得到了路径但我找不到我们保存 pdf 的路径 ....所以如何在我的应用程序中保存它

标签: iphone ios objective-c pdf


【解决方案1】:

我相信你可以简单地使用下面两条线:

NSData *myFile = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"your_url"]]; 
[myFile writeToFile:[NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] resourcePath], @"yourfilename.pdf"] atomically:YES];

【讨论】:

    【解决方案2】:

    希望对你有帮助 将pdf保存到应用程序中

    NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: path]]; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);         
    NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"pdfname.pdf"];      
    NSError *writeError = nil;   
         [imageData writeToFile:filePath options:NSDataWritingAtomic error:&writeError];
               if (writeError) {
                  NSLog(@"Error writing file: %@", writeError);    }
    

    从 NSDocument 目录获取 pdf

    NSString *stringPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
    
    NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory  error:&error];
    
    for(int i=0;i<[filePathsArray count];i++)
    {
        NSString *strFilePath = [filePathsArray objectAtIndex:i];
        if ([[strFilePath pathExtension] isEqualToString:@"pdf"]) 
        {
            NSString *pdfPath = [[stringPath stringByAppendingFormat:@"/"] stringByAppendingFormat:strFilePath];
            NSData *data = [NSData dataWithContentsOfFile:pdfPath];
            if(data)
            {
                UIImage *image = [UIImage imageWithData:data];
                [arrayOfImages addObject:image];
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多