How to dismiss camera controller from framework in iOS Swift?

1.4k views Asked by At

I have implemented vision document scanner inside framework. When camera view controller is called and document captured. While save button tapped it should dismiss and return to viewController.

Here is the code inside framework:

      public func showScanner(){
       self.createTaskController()
//        let scannerViewController = VNDocumentCameraViewController()
//        scannerViewController.delegate = self
//        present(scannerViewController, animated: true)
       print("Called Build")
 }

private func createTaskController(){
     let scannerViewController = VNDocumentCameraViewController()
     scannerViewController.delegate = self
     self.clientView?.addChild(scannerViewController)
     self.clientView?.view.addSubview(scannerViewController.view)
     scannerViewController.didMove(toParent: clientView)
     scannerViewController.dismiss(animated: true)
 }

public func imageFromFile(result: @escaping (_ image: UIImage?) -> Void){
         //the image
     if imageNew != nil { 
         result(imageNew)
         }
         else{
             //callback nil so the app does not pause infinitely if
             //the error != nil
         result(nil)
         }
  }

  public func documentCameraViewController(_ controller: VNDocumentCameraViewController, didFinishWith scan: VNDocumentCameraScan) {
        guard scan.pageCount >= 1 else {
            controller.dismiss(animated: true)
            return
        }

        let originalImage = scan.imageOfPage(at: 0)
        let newImage = compressedImage(originalImage)
        imageNew = newImage
        print("new image::\(newImage.size)")
        print("new imagei::\(newImage)")
        controller.dismiss(animated: true)
    }

    public func documentCameraViewController(_ controller: VNDocumentCameraViewController, didFailWithError error: Error) {
        print(error)
        controller.dismiss(animated: true)
    }

    public func documentCameraViewControllerDidCancel(_ controller: VNDocumentCameraViewController) {
        controller.dismiss(animated: true)
    }

    func compressedImage(_ originalImage: UIImage) -> UIImage {
        guard let imageData = originalImage.jpegData(compressionQuality: 1),

            let reloadedImage = UIImage(data: imageData) else {
                return originalImage
        }
        return reloadedImage
    }

Here is the code where i have called framework inside sample project:

 @IBAction func btnAction(_ sender: Any) {
    A8Scan(self).showScanner()
    p()
}

My issue is when tapping on save button it should dismiss camera controller (VNDocumentCameraViewController) and return to sample app. But, In my case its not returning.

Any help much appreciated pls...

1

There are 1 answers

3
Shehata Gamal On

You add it as a child here

let scannerViewController = VNDocumentCameraViewController()
private func createTaskController(){
    scannerViewController.delegate = self
    self.clientView?.addChild(scannerViewController)
    self.clientView?.view.addSubview(scannerViewController.view)
    scannerViewController.didMove(toParent: clientView)
 ///    scannerViewController.dismiss(animated: true) remove this line
}

then to remove do

scannerViewController.removeFromParent()
scannerViewController.view.removeFromSuperView()

OR

private func createTaskController(){ 
    let scannerViewController = VNDocumentCameraViewController()
    scannerViewController.delegate = self  
    self.clientView?.present(scannerViewController,animated:true,completion:nil)
}

Dismiss

controller.dismiss(animated: true)

To send the image create a function inside the clientView and call it

let newImage = compressedImage(originalImage)
self.clientView?.sendImage(newImage)