【问题标题】:Unable to share multiple images using share sheet Swift无法使用共享表 Swift 共享多个图像
【发布时间】:2021-01-19 23:42:29
【问题描述】:

我正在尝试保存使用视觉套件扫描的多张图像,但似乎只有第一张图像使用共享表保存。我该如何解决这个问题。

扫描后保存图片

for i in 0...scan.pageCount-1 {
      let originalImage = scan.imageOfPage(at: i)
      let fixedImage =  originalImage.jpegData(compressionQuality: 0.7)
      let reloadedImage = UIImage(data: fixedImage!)
      let imagetoshare = [reloadedImage!]
      
      let activityViewController = UIActivityViewController(activityItems: imagetoshare, 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 = []
      }
      // exclude some activity types from the list (optional)
      // present the view controller
      self.present(activityViewController, animated: true, completion: nil)
}

添加了弹出框控制器代码,因为 iPad 无法调出共享表...

尝试保存多张图片时出现错误代码 I

2020-10-03 16:17:21.300830+0530 Scan Box[1163:132306] [ShareSheet] connection invalidated

如果您想知道 reloadedImage 函数是什么:

func reloadedImage(_ originalImage: UIImage) -> UIImage {
      guard let imageData = originalImage.jpegData(compressionQuality: 1),
            let reloadedImage = UIImage(data: imageData) else {
                return originalImage
      }
      return reloadedImage
}

编辑: 我已从 pageCount 中删除了“-1”,但出现此错误:

-[VNDocumentCameraScan imageOfPageAtIndex:]: index (2) beyond bounds (2).'

编辑 2:

       var imagetoShare = [UIImage]()
                for i in 0...scan.pageCount-1 {
                    let originalImage = scan.imageOfPage(at: i)
                    let fixedImage =  originalImage.jpegData(compressionQuality: 0.7)
                    
                    let reloadedImage = UIImage(data: fixedImage!)
                    imagetoShare.append(reloadedImage!)
                  
                        
                        let activityViewController = UIActivityViewController(activityItems: imagetoShare, 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 = []
                            
                      }
                              
                              // exclude some activity types from the list (optional)


                              // present the view controller
                              self.present(activityViewController, animated: true, completion: nil)
                    

我什至尝试过:

imagetoShare: [UIImage] = [UIImage]()

但仍然没有运气

在我的调试器日志显示这些更改后,

2020-10-06 16:02:03.813409+0530 Scan Box[1706:336940] [] [16:02:03.811] SurfacePool_DetachSurface signalled err=-16990 (kFigPhotoError_InvalidParameter) (Surface not found in pool) at /Library/Caches/com.apple.xbs/Sources/EmbeddedCoreMedia/EmbeddedCoreMedia-2755.18.2.1.1/Sources/Photo/FigPhotoSurfacePool.c:1118
2020-10-06 16:02:06.469717+0530 Scan Box[1706:336940] [] [16:02:06.470] SurfacePool_DetachSurface signalled err=-16990 (kFigPhotoError_InvalidParameter) (Surface not found in pool) at /Library/Caches/com.apple.xbs/Sources/EmbeddedCoreMedia/EmbeddedCoreMedia-2755.18.2.1.1/Sources/Photo/FigPhotoSurfacePool.c:1118
2020-10-06 16:02:09.287991+0530 Scan Box[1706:336940] [Presentation] Attempt to present <UIActivityViewController: 0x102055600> on <UINavigationController: 0x102018e00> (from <Scan_Box.ViewController: 0x102019400>) while a presentation is in progress.
2020-10-06 16:02:09.290234+0530 Scan Box[1706:336940] [Presentation] Attempt to present <UIActivityViewController: 0x101a0f200> on <UINavigationController: 0x102018e00> (from <Scan_Box.ViewController: 0x102019400>) while a presentation is in progress.
2020-10-06 16:02:09.292873+0530 Scan Box[1706:337619] [ShareSheet] connection invalidated
2020-10-06 16:02:09.294701+0530 Scan Box[1706:338340] [ShareSheet] connection invalidated
2020-10-06 16:02:15.780167+0530 Scan Box[1706:338348] [ShareSheet] connection invalidated

请帮我解决这个问题。 提前致谢!

【问题讨论】:

    标签: ios swift uiactivityviewcontroller visionkit


    【解决方案1】:

    您创建了一堆 UIActivityViewController 以在您的代码中显示在您的循环中,因此只会显示其中的第一个,因为会跳过下一个。 您应该首先创建要共享的图像数组,然后使用该数组显示控制器。

    // Prepare array of images to share
    var images = [UIImage]()
    for i in 0...scan.pageCount-1 {
        ...
        images.append(reloadedImage!)
    }
    // Create and present single activity controller
    let activityViewController = UIActivityViewController(activityItems: images, applicationActivities: nil)
    ...
    self.present(activityViewController, animated: true, completion: nil)
    

    【讨论】:

    • 不,它仍然不起作用:(检查我的调试器日志编辑。
    • 我也有同样的问题,请多提建议,可能会有帮助。
    • @Manav103 只是从循环中移出创建并展示您的 UIActivityViewController。
    • 非常感谢@iUrii,这对我很有帮助。如此简单的修复!
    猜你喜欢
    • 2021-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多