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

十年热恋 提交于 2019-12-23 10:03:16

问题


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


回答1:


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

}



回答2:


As you are using a middle man (A8Scan) NSObject to present a controller. the A8Scan will get deallocated immediately after presenting the controller which you can confirm by keeping a deinit function inside A8Scan.

A workaround for this is to make your NSObject (A8Scan) as a singleton ie add

static let shared = A8Scan()
var callBack: ((_ image: UIImage?) -> Void)?

remove the existing init method and add the viewcontroller property into the imageFromFile: method, your function will look like this

public func imageFromFile(sender: UIViewController, result: @escaping (_ image: UIImage?) -> Void){
   let scannerViewController = VNDocumentCameraViewController()
   scannerViewController.delegate = self
   self.callBack = result
   sender.present(scannerViewController,animated:true,completion: 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
    set(image: imageNew!)
    callBack?(imageNew!)
    controller.dismiss(animated: true, completion: nil)
}

and finally in your sample project,

 @IBAction func btnAction(_ sender: Any) {
  A8Scan.shared.imageFromFile(sender: self) { (image) in
     if image != nil {
         print(image!)
     } else {
         print("something went wrong")
     }
   }
 }


来源:https://stackoverflow.com/questions/59248664/how-to-send-image-from-framework-to-sample-project-in-ios-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!