Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send image from framework to sample project in iOS Swift?

I have implemented document scanner using visionKit. Initially i have faced camera dismiss issue and it fixed. Now i am trying to send image after camera dismiss from framework to sample project.

I have tried using completion handler, but it does not work.

Here is the code for framework:

public class A8Scan: NSObject, VNDocumentCameraViewControllerDelegate {

var imageNew: UIImage?
var statusImage: UIImageView?
private var clientView: UIViewController?
public init(_ viewController:UIViewController){

          self.clientView = viewController

   }

public func showScanner(imgData: UIImage?){

    self.createTaskController(img: imgData)

       print("Called Build")


 }

private func createTaskController(img: UIImage?){

     let scannerViewController = VNDocumentCameraViewController()
     scannerViewController.delegate = self
    self.clientView?.present(scannerViewController,animated:true,completion:  {

            self.imageNew = img

    })


 }

public func imageFromFile(result: @escaping (_ image: UIImage?) -> Void){

         //the image
    let scannerViewController = VNDocumentCameraViewController()
    scannerViewController.delegate = self
    self.clientView?.present(scannerViewController,animated:true,completion: nil)

     if imageNew != nil {

         result(imageNew)
         }
         else{
             //callback nil so the app does not pause infinitely if
             //the error != nil
         result(nil)
         }

 }

func set(image: UIImage) {

    self.statusImage?.image = image

}

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
        set(image: imageNew!)
        print("new image::\(newImage.size)")
        controller.dismiss(animated: true, completion: nil)

//        processImage(newImage)
    }

    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 sample project:

     @IBAction func btnAction(_ sender: Any) {

 //        A8Scan(self).showScanner()
 //        A8Scan(self).showScanner(imgData: im)
 //        print("nn", A8Scan(self).showScanner(imgData: im))

    A8Scan(self).imageFromFile { (image) in

        if image != nil {

            print(image!)
        } else {

            print("something went wrong")
        }
    }
//        p()
}

func p (){
    ScannerViewController().imageFromFile{(image: UIImage?) -> Void in
        //use the image that was just retrieved

        print("image data", image)
    }
}

My issue is after camera dismiss from framework it does not send image from framework to sample project.

Any help much appreciated pls....

like image 723
PvUIDev Avatar asked Oct 15 '22 08:10

PvUIDev


1 Answers

1- Add this inside your framework

var callBack((UIImage)-())?

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

2-

let newImage = compressedImage(originalImage)
callBack?(newImage)

3-

Then inside the vc that uses it

let vc = A8Scan(self)
vc.show()
vc.callBack = { [weak self] image in

}
like image 195
Sh_Khan Avatar answered Oct 23 '22 19:10

Sh_Khan