我正在尝试使用 Firebase ML Kit 在 iOS 中构建一个文本识别器应用程序。我尝试过一些教程,但没有运气。我在指示的行中不断收到以下错误(返回 self.result):
线程 1:致命错误:在隐式展开可选值时意外发现 nil
我对 Swift/xcode 和 firebase 还是很陌生,所以任何帮助都将不胜感激!
var result: VisionText!
var textRecognizer: VisionTextRecognizer!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let vision = Vision.vision()
textRecognizer = vision.cloudTextRecognizer()
imageResult.image = UIImage(named: "sampletext")
print(textRecognition(image: imageResult.image!))
textResult.text += scantext
}
func textRecognition(image: UIImage) -> VisionText{
let visionImage = VisionImage(image: image)
textRecognizer.process(visionImage) { (result, error) in guard error == nil, case self.result = result else {
print("oops")
return
}
print("oops")
}
return self.result \\ ERROR
}
编辑
我确保采用正确的方法来解开可选选项。我的问题是 Firebase MLVision 进程没有返回任何内容,结果为零。不确定我是否错误地使用了该方法。这是我更新的代码,有一些小的改动。
var scannedresult: VisionText!
var textRecognizer: VisionTextRecognizer!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let vision = Vision.vision()
textRecognizer = vision.cloudTextRecognizer()
imageResult.image = UIImage(named: "sampletext")
print("oops")
print(textRecognition(image: imageResult.image!))
// textResult.text += scannedresult.text
}
func textRecognition(image: UIImage) {
let visionImage = VisionImage(image: image)
textRecognizer.process(visionImage) { (result, error) in guard error == nil, let result = result else { print("oops")
return }
self.scannedresult = result
}
}