I am trying to save multiple images scanned using vision kit but only the first image seems to save using the share sheet. How do I rectify this issue.
Saving the image after scanning
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)
}
the popover controller code was added as iPads have issues with bringing up the share sheet...
Error Code I when I attempt to save multiple images
2020-10-03 16:17:21.300830+0530 Scan Box[1163:132306] [ShareSheet] connection invalidated
In case you wanted to know what the reloadedImage function is:
func reloadedImage(_ originalImage: UIImage) -> UIImage {
guard let imageData = originalImage.jpegData(compressionQuality: 1),
let reloadedImage = UIImage(data: imageData) else {
return originalImage
}
return reloadedImage
}
EDIT: I have removed the '-1' from pageCount and I get this error:
-[VNDocumentCameraScan imageOfPageAtIndex:]: index (2) beyond bounds (2).'
EDIT 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)
I even tried:
imagetoShare: [UIImage] = [UIImage]()
but still no luck
After these changes my debugger log shows,
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
Please help me resolve this issue. Thanks in advance!
You create a bunch of UIActivityViewController to show inside your loop from your code so only first of them will be shown because next are skipped. You should create array of images to share first and then show the controller with this array.