当前,我正在使用ARKit / CoreML / Vision来识别图像/对象。
为此,我看了一下Apple的示例项目Recognizing and Labeling Arbitrary Objects
我已将以下行从ViewController.Swift脚本复制到我的项目中:
[...]
private func classifyCurrentImage() {
// Most computer vision tasks are not rotation agnostic so it is important to pass in the orientation of the image with respect to device.
let orientation = CGImagePropertyOrientation(UIDevice.current.orientation)
[...]
}
这里是CGImagePropertyOrientation类型的常量。
当我尝试将设备方向线作为参数传递时,会出现错误。由于CGImagePropertyOrientation期望的值为UInt32类型,而不是UIDeviceOrientation
编译器错误输出:
无法将类型'UIDeviceOrientation'的值转换为预期的参数类型'UInt32'
我认为错误在此处UIDevice.current.orientation
您对UInt32是正确的,因为UIDeviceOrientation是具有Int类型的枚举。我认为将设备方向的原始值转换/转换为UInt32将解决您的问题。
尝试下面的代码
CGImagePropertyOrientation(rawValue: UInt32(UIDevice.current.orientation.rawValue))
@ ibnetariq的第一个答复解决了此代码段中的问题。但是我找到了另一个解决方案。示例项目包含CGImagePropertyOrientation的扩展,可以解决我的问题。
代码段Utitlity.swift
import UIKit
import ImageIO
extension CGImagePropertyOrientation {
/**
Converts a `UIImageOrientation` to a corresponding
`CGImagePropertyOrientation`. The cases for each
orientation are represented by different raw values.
- Tag: ConvertOrientation
*/
init(_ orientation: UIImageOrientation) {
switch orientation {
case .up: self = .up
case .upMirrored: self = .upMirrored
case .down: self = .down
case .downMirrored: self = .downMirrored
case .left: self = .left
case .leftMirrored: self = .leftMirrored
case .right: self = .right
case .rightMirrored: self = .rightMirrored
}
}
}