Swift 是否可以使用iOS 11.3.1上通过编程从图像创建的ARReferenceImage对象

Swift 是否可以使用iOS 11.3.1上通过编程从图像创建的ARReferenceImage对象,swift,ios11,arkit,Swift,Ios11,Arkit,我最近用本地ARKit替换了一个沉重的OpenCV,用于图像检测和跟踪。我要跟踪的图像是从web服务下载并存储在本地的。我正在使用以下代码从它们创建ARImageReference对象: guard let image = UIImage(contentsOfFile: imageLocalPath), let cgImage = image.cgImage else { return nil } return ARReferenceImage(cgImage, ori

我最近用本地ARKit替换了一个沉重的OpenCV,用于图像检测和跟踪。我要跟踪的图像是从web服务下载并存储在本地的。我正在使用以下代码从它们创建
ARImageReference
对象:

guard
    let image = UIImage(contentsOfFile: imageLocalPath),
    let cgImage = image.cgImage
else {
    return nil
}

return ARReferenceImage(cgImage, orientation: .up, physicalWidth: 0.12)
宽度很小,因为图像也不太大,每个大约180 x 240像素,但打印的图像可能更大

根据当前iOS版本配置的会话,因为iOS 11不可用
ARImageTrackingConfiguration

private lazy var configuration: ARConfiguration = {
    if #available(iOS 12.0, *),
        ARImageTrackingConfiguration.isSupported {
        return ARImageTrackingConfiguration()
    }

    return ARWorldTrackingConfiguration()
}()

if #available(iOS 12.0, *),
    let imagesTrackingConfig = configuration as? ARImageTrackingConfiguration {
    imagesTrackingConfig.trackingImages = referenceImages
} else if let worldTrackingConfig = configuration as? ARWorldTrackingConfiguration {
    worldTrackingConfig.detectionImages = referenceImages
}

session.run(configuration, options: [.resetTracking, .removeExistingAnchors])
即使我使用了
arworldctrackingconfiguration
,上面的代码对于iOS版本12和13也非常有效。ARKit正确检测到的图像。但当我尝试在iOS 11.3.1上运行它时,应用程序立即崩溃,出现以下错误:

断言: /BuildRoot/Library/Caches/com.apple.xbs/Sources/AppleCV3D/AppleCV3D-1.13.11/Library/VIO/OdometryEngine/src/FrameDownsampleNode/FrameDownsampler.cpp, 62:std::abs(静态演员阵容(方面1)- 静态投影(src\u frame.image.width*输出\u frame\u height)< 最大松弛(lldb)


是否有可能以编程方式创建的动态标记不适用于12.0以下的iOS版本,或者我做错了什么?不幸的是,我找不到任何关于特定版本的信息。任何反馈都将不胜感激。谢谢。

过了一段时间,我终于找到了问题的根源,因此我将其发布在这里,因为它可能对其他有同样问题的人有用

答案是,可以通过编程方式创建
ARReferenceImage
对象,并在iOS 11.3.1上用于图像检测

我的问题是源图像本身,看起来是
CGImage
的比例不当导致了崩溃。从API下载的图像为180x240。通过检查用于创建
ARReferenceImage
CGImage
的大小,我发现它们没有正确缩放,因此
CGImage
大小与源
UIImage
相同

我决定尝试使用以下方法重新绘制图像:

func getRedrawnCgImage(_ originalImage: UIImage) -> CGImage? {
    guard let cgImage = originalImage.cgImage else { return nil }

    let size = CGSize(width: cgImage.width, height: cgImage.height)
    
    UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
    originalImage.draw(in: CGRect(origin: .zero, size: size))
    let redrawnImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return redrawnImage?.cgImage
}
结果
CGImage
的大小为540x720,因此使用原始大小的x3比例。通过使用这些重新绘制的图像,我摆脱了崩溃,但这是一个解决办法,而不是目前的正确解决方案