gpt4 book ai didi

ios - 如何从文档目录加载 ARReferenceImage?

转载 作者:搜寻专家 更新时间:2023-10-31 08:28:21 25 4
gpt4 key购买 nike

目前,我正在使用下面的代码从 Assets.xcassets 的“AR Resources”加载 ARReferenceImage,

guard let trackingImages =  ARReferenceImage.referenceImages(inGroupNamed: "AR Resources", bundle: nil) else {
print("Missing expected asset catalog resources.")
return
}
configuration.trackingImages = trackingImages

现在,我正在从服务器下载图像到 iOS 应用程序文档目录中名为“Images”的自定义文件夹。

如何将这些图像用作“跟踪图像”?

最佳答案

为了加载自定义ARReference来自指定位置而非默认位置的图像 ARAssets Folder您需要使用以下方法创建它们:

init(CGImage, orientation: CGImagePropertyOrientation, physicalWidth: CGFloat)

在此示例中,我使用 ARWorldTrackingConfiguration但如果使用 ARImageTrackingConfiguration 的原则是一样的。

假设您将所有下载的图像存储到 [UIImage] 数组中,例如:

let imagesFromServer: [UIImage] = [UIImage(named: "gitHubIcon")!, UIImage(named: "stackOverflowIcon")!]

然后你可以把这些变成ARReferenceImages使用这样的东西:

/// Converts An Array Of [UIImage] Into A Set Of ARReferenceImages
///
/// - Parameter images: [UIImage])
/// - Returns: Set<ARReferenceImage>
func loadedImagesFromDirectoryContents(_ images: [UIImage]) -> Set<ARReferenceImage>{

var index = 0
var customReferenceSet = Set<ARReferenceImage>()

images.forEach { (downloadedImage) in

//1. Convert The UIImage To A CGImage
guard let cgImage = downloadedImage.cgImage else { return }

//2. Get The Width Of The Image
let imageWidth = CGFloat(cgImage.width)

//3. Create A Custom AR Reference Image With A Unique Name
let customARReferenceImage = ARReferenceImage(cgImage, orientation: CGImagePropertyOrientation.up, physicalWidth: imageWidth)
customARReferenceImage.name = "MyCustomARImage\(index)"

//4. Insert The Reference Image Into Our Set
customReferenceSet.insert(customARReferenceImage)

print("ARReference Image == \(customARReferenceImage)")

index += 1


}


//5. Return The Set
return customReferenceSet

}

记住 ARWorldTrackingConfiguration需要 Set<ARReferenceImage> .

然后您可以初始化您的自定义 configurations detectionImages像这样:

 let detectionImages = loadedImagesFromDirectoryContents(imagesFromServer)

configuration.detectionImages = detectionImages

augmentedRealitySession.run(configuration, options: [.resetTracking, .removeExistingAnchors])

希望它能为您指明正确的方向...

关于ios - 如何从文档目录加载 ARReferenceImage?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51998613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com