Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 无法使用共享工作表共享PDF文件_Ios_Swift - Fatal编程技术网

Ios 无法使用共享工作表共享PDF文件

Ios 无法使用共享工作表共享PDF文件,ios,swift,Ios,Swift,我正在尝试使用vision kit创建PDF,并使用共享表进行共享。 创建PDF后,共享表将显示并显示选项,但当我继续单击其中一个项目时,会出现一个错误,提示“无法共享此项目。请选择其他项目。” 目前我在代码或终端中找不到任何差异 以下是用于创建和共享PDF的代码: func createNewPDF(arrImage: [UIImage]) -> Data? { let pdfDocument = PDFDocum

我正在尝试使用vision kit创建PDF,并使用共享表进行共享。 创建PDF后,共享表将显示并显示选项,但当我继续单击其中一个项目时,会出现一个错误,提示“无法共享此项目。请选择其他项目。”

目前我在代码或终端中找不到任何差异

以下是用于创建和共享PDF的代码:

   
        func createNewPDF(arrImage: [UIImage]) -> Data? {
            
            let pdfDocument = PDFDocument()
            for i in 0...arrImage.count-1 {
                let pdfPage = PDFPage(image: arrImage[i])
                pdfDocument.insert(pdfPage!, at: i)
            }
            let pdfData = pdfDocument.dataRepresentation()
            
            

            let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).map(\.path)
            let documentsDirectory = paths[0]
            let fullPath = URL(fileURLWithPath: documentsDirectory).appendingPathComponent("\(pdfnameing).pdf") as URL
            let fullPathf = URL(fileURLWithPath: documentsDirectory).appendingPathComponent("\(pdfnameing).pdf").path
            let urll = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("\(pdfnameing).pdf") as NSURL
            do {
                   let data = try Data(contentsOf: fullPath)

                   try data.write(to: urll as URL)

                   let activitycontroller = UIActivityViewController(activityItems: [urll], applicationActivities: nil)
                   if activitycontroller.responds(to: #selector(getter: activitycontroller.completionWithItemsHandler))
                   {
                       activitycontroller.completionWithItemsHandler = {(type, isCompleted, items, error) in
                           if isCompleted
                           {
                               print("completed")
                           }
                       }
                   }
                
                if let popoverController = activitycontroller.popoverPresentationController {
                popoverController.sourceView = self.view
                  popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
                  popoverController.permittedArrowDirections = []

              }
                   activitycontroller.popoverPresentationController?.sourceView = self.view
                   self.present(activitycontroller, animated: true, completion: nil)

               }
               catch {
                   //ERROR
               }
            

           let activityItems = [tempURL]
        let activityViewController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)

     if let popoverController = activityViewController.popoverPresentationController {
           popoverController.sourceView = self.view
          popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
       popoverController.permittedArrowDirections = []

  }

   self.present(activityViewController, animated: true, completion: nil)

            
            return pdfData
        }

“pdfnameing”是用于命名PDF文件的字符串

提前谢谢你的帮助

编辑: 在建议更改之后,我仍然得到了与上图所示相同的错误

该代码包含以下更改:


        
        func createNewPDF(arrImage: [UIImage]) -> Data? {
            
            let pdfDocument = PDFDocument()
            for i in 0...arrImage.count-1 {
                let pdfPage = PDFPage(image: arrImage[i])
                pdfDocument.insert(pdfPage!, at: i)
            }
            let pdfData = pdfDocument.dataRepresentation()
            let tempURL = FileManager.default.temporaryDirectory
                .appendingPathComponent("\(pdfnameing).pdf")
            do {
         

                   let activitycontroller = UIActivityViewController(activityItems: [tempURL], applicationActivities: nil)
                   if activitycontroller.responds(to: #selector(getter: activitycontroller.completionWithItemsHandler))
                   {
                       activitycontroller.completionWithItemsHandler = {(type, isCompleted, items, error) in
                           if isCompleted
                           {
                               print("completed")
                           }
                       }
                   }

                if let popoverController = activitycontroller.popoverPresentationController {
                popoverController.sourceView = self.view
                  popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
                  popoverController.permittedArrowDirections = []

              }
                   activitycontroller.popoverPresentationController?.sourceView = self.view
                   self.present(activitycontroller, animated: true, completion: nil)

               }
               catch {
                   //ERROR
               }
            
      let anURL = URL(fileURLWithPath: tempURL)

           let activityItems = [anURL]
            
        let activityViewController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)

     if let popoverController = activityViewController.popoverPresentationController {
           popoverController.sourceView = self.view
          popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
       popoverController.permittedArrowDirections = []

  }

   self.present(activityViewController, animated: true, completion: nil)



            return pdfData
        }
    
调试器日志:

The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2020-11-03 15:57:48.058158+0530 Scan Box[4068:1060013] [] Trying to load recrop for scan, but the quad is nil, setting starting UI recrop to a 95% unit rect
Success
2020-11-03 15:58:10.127405+0530 Scan Box[4068:1060013] [core] SLRemoteComposeViewController: (this may be harmless) viewServiceDidTerminateWithError: Error Domain=_UIViewServiceErrorDomain Code=1 "(null)" UserInfo={Terminated=disconnect method}

中列出的UIView上UIConstraintBasedLayoutDebugging类别中的方法也可能有用。 2020-11-03 15:57:48.058158+0530扫描框[4068:1060013][]尝试加载重新扫描以进行扫描,但四元组为零,将启动UI重新扫描设置为95%单位矩形 成功 2020-11-03 15:58:10.127405+0530扫描框[4068:1060013][core]SLRemoteComposeViewController:(这可能是无害的)viewservicedidtterminatewitherror:Error Domain=\u UIViewServiceErrorDomain Code=1“(null)”UserInfo={Terminated=disconnect method}
您从未保存过pdfData。有很多非检测代码,您试图从磁盘加载数据,但pdfData变量中已经有数据

把那些线去掉就行了

let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).map(\.path)
let documentsDirectory = paths[0]
let fullPath = URL(fileURLWithPath: documentsDirectory).appendingPathComponent("\(pdfnameing).pdf") as URL
let fullPathf = URL(fileURLWithPath: documentsDirectory).appendingPathComponent("\(pdfnameing).pdf").path
let urll = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("\(pdfnameing).pdf") as NSURL
do {
    let data = try Data(contentsOf: fullPath)
    try data.write(to: urll as URL)

并将其替换为:

let pdfData = pdfDocument.dataRepresentation()
let tempURL = FileManager.default.temporaryDirectory
    .appendingPathComponent("\(pdfnameing).pdf")
do {
    try pdfData.write(to: tempURL) 

我已经做了编辑。请看一看,谢谢你的帮助!难怪它不起作用。您没有删除这一行
let data=try data(contentsOf:tempURL)
我删除了这一行,但它仍然不起作用。。。。我添加了断点,但我发现它没有在“do”括号内运行任何东西,只在“catch”之后运行代码,这会给我上图所示的错误……请尝试打印
Array(pdfData)