gpt4 book ai didi

ios - 如何在 iOS Swift 中将图像从框架发送到示例项目?

转载 作者:行者123 更新时间:2023-12-04 20:26:42 30 4
gpt4 key购买 nike

我已经使用visionKit 实现了文档扫描仪。最初我遇到了相机关闭问题并且它已修复。现在我试图在相机从框架中关闭后将图像发送到示例项目。

我曾尝试使用完成处理程序,但它不起作用。

这是框架的代码:

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
}

}

这是代码示例项目:
     @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)
}
}

我的问题是从框架中关闭相机后,它不会将图像从框架发送到示例项目。

任何帮助非常感谢请....

最佳答案

1-
将此添加到您的框架中

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-

然后在使用它的vc里面
let vc = A8Scan(self)
vc.show()
vc.callBack = { [weak self] image in

}

关于ios - 如何在 iOS Swift 中将图像从框架发送到示例项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59248664/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com