我正在调用AVFoundation的委托方法来处理照片捕获,但是我很难将它生成的AVCapturePhoto转换为具有正确方向的UIImage.虽然下面的例程是成功的,但我总是得到一个面向右侧的UIImage(UIImage.imageOrientation = 3).使用UIImage(数据:图像)并尝试首先使用photo.cgImageRepresentation()时,我无法提供方向?takeRetainedValue()也没有帮助.请协助.
在此处,图像方向至关重要,因为生成的图像将被输入Vision框架工作流程.
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
// capture image finished
print("Image captured.")
if let imageData = photo.fileDataRepresentation() {
if let uiImage = UIImage(data: imageData){
// do stuff to UIImage
}
}
}
Run Code Online (Sandbox Code Playgroud)
更新1: 阅读Apple的Photo Capture编程指南(iOS11过时),我确实找到了一件我做错的事情:

更新2 链接到GitHub上的完整项目:https://github.com/agu3rra/Out-Loud
我正在为 iOS 应用程序运行 Vision Framework 请求,如下所示:
let textDetectionRequest = VNDetectTextRectanglesRequest(completionHandler: self.findTextBoxes)
let textDetectionHandler = VNImageRequestHandler(cgImage: image, orientation: orientation, options: [:])
DispatchQueue.global(qos: .background).async {
do {
try textDetectionHandler.perform([textDetectionRequest])
} catch {
print(error)
}
}
Run Code Online (Sandbox Code Playgroud)
它工作正常,我正在使用通知/观察者模式在请求完成后采取进一步的操作。有时,在应用程序中,我需要在请求完成其操作之前取消进程,那么我可以使用 Vision Framework 中的任何类型的协议/委托方法来调用取消吗?
谢谢。
我目前正在使用 AVFoundation 捕获图像。由于我想在 Vision Framework 工作流程中使用捕获的图像,因此在将其转换为 UIImage 时需要其方向。我怎样才能做到这一点?从文档中我发现 AVCapturePhoto 有一个 .metadata 字典来访问该信息,但如果我使用相应的密钥,结果为零。
这是我的捕获例程的委托方法:
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
// capture image finished
print("Image captured.")
print(photo)
print(photo.metadata["kCGImagePropertyOrientation"]) // CGImageProperties for metadata keys for value retrieval.
Run Code Online (Sandbox Code Playgroud)
我在“CGImageProperties>Individual Image Properties”下找到了该键。打印(照片)确实向我显示了实际上已捕获的图像,返回:
AVCapturePhoto:0x1c1013940 点:98386.095931 1/1 设置:uid:3 照片:{4032x3024 SIS:ON}
请用 Swift 回答。:)
谢谢!
我已经创建了以下链接中提供的 Xcode 项目,以便探索 Apple 使用 Xcode9 发布的新 Vision Framework。它基于我在 GitHub 和 Youtube 上找到的代码所做的调整,但我无法理解为什么 VNImageRequestHandler 上的方向参数上的 CGImagePropertyOrientation.right 。有人可以阐明这一点吗?