无法使用 share sheet Swift 共享多张图片

Unable to share multiple images using share sheet Swift

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

扫描后保存图片

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 在调出共享方面存在问题 sheet...

尝试保存多张图片时出现错误代码 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

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

您创建了一堆 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)