gpt4 book ai didi

json - ARKit 4.0 – 是否可以将 ARWorldMap 数据转换为 JSON 文件?

转载 作者:行者123 更新时间:2023-12-03 21:49:28 26 4
gpt4 key购买 nike

我想知道是否可以转换 worldMap二进制数据(存储空间映射状态和一组 ARAnchor)到 jsonxml文件?

func writeWorldMap(_ worldMap: ARWorldMap, to url: URL) throws {

let data = try NSKeyedArchiver.archivedData(withRootObject: worldMap,
requiringSecureCoding: true)
try data.write(to: url)
}
如果可能,我可以使用哪些工具?

最佳答案

恐怕唯一的方法就是将 ARWorldMap 包裹起来。对象变成 Codable像这样的对象:

struct ARData {
var worldMap: ARWorldMap?
}

extension ARData: Codable {
enum CodingKeys: String, CodingKey {
case worldMap
}

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

let worldMapData = try container.decode(Data.self, forKey: .worldMap)
worldMap = try NSKeyedUnarchiver.unarchivedObject(ofClass: ARWorldMap.self, from: worldMapData)
}

func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

if let worldMap = worldMap {
let colorData = try NSKeyedArchiver.archivedData(withRootObject: worldMap, requiringSecureCoding: true)
try container.encode(colorData, forKey: .worldMap)
}
}
}
要将该对象的实例编码为 JSON,请使用 encode(:) JSONEncoder的功能:
let arData = ARData(worldMap: worldMap)
let encoder = JSONEncoder()

do {
let jsonData = try encoder.encode(arData)
} catch {
print(error)
}
默认情况下, JSONEncoder将转换 ARWorldMap数据转换为可以使用 JSONDecoder 读取的 Base64 字符串:
let decoder = JSONDecoder()

do {
let decoded = try decoder.decode(ARData.self, from: jsonData)
} catch {
print(error)
}

关于json - ARKit 4.0 – 是否可以将 ARWorldMap 数据转换为 JSON 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63247478/

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