0

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...

Maulik Pandya
  • 2,200
  • 17
  • 26
PvUIDev
  • 95
  • 1
  • 9
  • 38

1 Answers1

1

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)
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • actually i have written completion handler for 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) } } – PvUIDev Dec 09 '19 at 10:51
  • how to call completion handler after camera dismiss in sample app – PvUIDev Dec 09 '19 at 10:52
  • If i use present image captured are not available. And if i use removefromparent camera view got struct. – PvUIDev Dec 10 '19 at 05:05