iOS ARKIT,Vision,CoreMl CGImagePropertyOrientation预期类型错误

问题描述 投票:0回答:2

当前,我正在使用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

ios swift arkit coreml vision
2个回答
0
投票

您对UInt32是正确的,因为UIDeviceOrientation是具有Int类型的枚举。我认为将设备方向的原始值转换/转换为UInt32将解决您的问题。

尝试下面的代码

CGImagePropertyOrientation(rawValue: UInt32(UIDevice.current.orientation.rawValue))

0
投票

@ 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
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.