我正在尝试创建一个应用程序,使用 MLKit 的文本检测功能检测设备相机拍摄的照片中的文本。下面是我的 photoOutput 方法中的代码,以及它调用的方法的代码:
func photoOutput(_ output: AVCapturePhotoOutput,
didFinishProcessingPhoto photo: AVCapturePhoto,
error: Error?) {
print("worked")
PHPhotoLibrary.shared().performChanges( {
let creationRequest = PHAssetCreationRequest.forAsset()
creationRequest.addResource(with: PHAssetResourceType.photo, data: photo.fileDataRepresentation()!, options: nil)
}, completionHandler: nil)
let cgImage = photo.cgImageRepresentation()!.takeRetainedValue()
print(cgImage)
let orientation = photo.metadata[kCGImagePropertyOrientation as String] as! NSNumber
let uiOrientation = UIImage.Orientation(rawValue: orientation.intValue)!
let image = UIImage(cgImage: cgImage, scale: 1, orientation: uiOrientation)
self.runTextRecognition(with: image)
}
func runTextRecognition(with image: UIImage) {
let visionImage = VisionImage(image: image)
textRecognizer.process(visionImage) { features, error in
self.processResult(from: features, error: error)
}
}
func processResult(from text: VisionText?, error: Error?) {
guard error == nil, let text = text else {
print("oops")
return
}
print(text.text)
}
每当我运行应用程序并拍照时,一切都运行良好,直到出现 textRecognizer.process(visionImage) 行。控制台消息是 -[Not A Type _cfTypeID]: message sent to deallocated instance 0x106623e20。
任何帮助或建议将不胜感激!如果我应该包含更多信息,请告诉我。