-3

When I try saving a scan to the camera roll the app crashes

Here's the Code:

func documentCameraViewController(_ controller: VNDocumentCameraViewController, didFinishWith scan: VNDocumentCameraScan) {
    // Make sure the user scanned at least one page
    guard scan.pageCount >= 1 else {

        // You are responsible for dismissing the VNDocumentCameraViewController.
        controller.dismiss(animated: true)
        return
    }

    // This is a workaround for the VisionKit bug which breaks the `UIImage` returned from `VisionKit`
    // See the `Image Loading Hack` section below for more information.

    var arrImages = [UIImage]()
    for i in 0...scan.pageCount-1 {
        let originalImage = scan.imageOfPage(at: i)
        let fixedImage = reloadedImage(originalImage)
        arrImages.append(fixedImage)
    }

    controller.dismiss(animated: true)

    let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
    let docURL = documentDirectory.appendingPathComponent("Delete This")

    if Filetype == 1 {
        let data = createNewPDF(arrImage: arrImages)
        do {
            try data?.write(to: docURL, options: .completeFileProtection)
            print("Success")

        } catch(let error) {
            print("error is \(error.localizedDescription)")
        }
    } else {
        if Filetype == 2 {

            if customjg == 68 {
                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!)
                    UIImageWriteToSavedPhotosAlbum(reloadedImage!, nil, nil, nil);
                    //arrImages.append(fixedImage)
                }
                if customjg == 69 {

                    let originalImage = scan.imageOfPage(at: 1)
                    let rere =  self.resizeImagezz(image: originalImage, targetSize: CGSize(width: Widthv, height: Heightv))
                    let fixedImage = rere.jpegData(compressionQuality: 0.7)
                    let reloadedImage = UIImage(data: fixedImage!)
                    UIImageWriteToSavedPhotosAlbum(reloadedImage!, nil, nil, nil);
                    //arrImages.append(fixedImage)
                }

            }
        }else{
            if Filetype == 3 {
                for i in 0...scan.pageCount-1 {
                    let originalImage = scan.imageOfPage(at: i)
                    let fixedImage =  originalImage.pngData()
                    let reloadedImage = UIImage(data: fixedImage!)
                    UIImageWriteToSavedPhotosAlbum(reloadedImage!, nil, nil, nil);
                    //arrImages.append(fixedImage)
                }
            }
        }
    }
}

The File Type is a segment controlled switch case. The first option by default is JPEG. It does not even ask for the camera roll access permission before crashing (Yes I've put it in the info.plist file).

Only PDF works as of now.

But the twist is that everything works when installed on iOS 14 Beta.

Please help me rectify this issue As soon as you can.

Thanks for the help in Advance.

Andrew
  • 26,706
  • 9
  • 85
  • 101
  • You've got quite a bit of forced unwrapping, have you thought about unwrapping the items before using them as forced unwraps that are nil will cause a crash? – Andrew Jul 06 '20 at 17:07
  • You should add the info about the crash and point out the line that’s causing it if you’re looking for help with a crash. – EmilioPelaez Jul 06 '20 at 17:09
  • Which line is causing the crash exactly? Is there any error message in console? Do you know that each `!` is yelling: crash if it's nil? You need to pinpoint the issue, that's an IMPORTANT part of debugging. – Larme Jul 06 '20 at 17:15
  • regardless of the crash, you should use Photos framework which is much more modern and easy to use. – Roi Mulia Jul 06 '20 at 22:24

1 Answers1

1

As per documentation - https://developer.apple.com/documentation/uikit/1619125-uiimagewritetosavedphotosalbum, we should implement the completionSelector. and for the same set completionTarget as self. implement the api as below:

UIImageWriteToSavedPhotosAlbum(reloadedImage!, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)

then in this completionSelector:

@objc func image(_ image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {
    guard let error = error else {//success return}
    // found error
    print(error)
}
Ankit Thakur
  • 4,739
  • 1
  • 19
  • 35