【发布时间】:2019-01-05 09:53:10
【问题描述】:
我正在尝试保存并将 ARKit ARWorldMap 加载到本地文件。我的保存工作似乎很好:
func saveWorldMap() {
ARView?.session.getCurrentWorldMap { [unowned self] worldMap, error in
guard let map = worldMap else { return }
do {
let data = try NSKeyedArchiver.archivedData(withRootObject: map, requiringSecureCoding: true)
do {
let url: URL = URL(fileURLWithPath: self.worldMapFilePath("test"))
try data.write(to: url)
} catch {
fatalError("Can't write to url")
}
} catch {
fatalError("Can't encode map")
}
}
}
func worldMapFilePath(_ fileName: String) -> String {
createWorldMapsFolder()
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = paths[0] as String
let filePath: String = "\(documentsDirectory)/WorldMaps/WorldMap_\(fileName)"
if FileManager().fileExists(atPath: filePath) { try! FileManager().removeItem(atPath: filePath) }
return filePath
}
func createWorldMapsFolder() {
let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first
if let documentDirectoryPath = documentDirectoryPath {
let replayDirectoryPath = documentDirectoryPath.appending("/WorldMaps")
let fileManager = FileManager.default
if !fileManager.fileExists(atPath: replayDirectoryPath) {
do {
try fileManager.createDirectory(atPath: replayDirectoryPath, withIntermediateDirectories: false, attributes: nil)
} catch {
print("Error creating Captures folder in documents dir: \(error)")
}
} else {
print("WorldMaps folder already created. No need to create.")
}
}
}
当我尝试加载保存的 ARWorldMap 时出现问题:
func loadWorldMap() {
guard let data = retrieveWorldMapDataForFilename("test") else { fatalError("Can't get data") }
do {
let worldMap = try NSKeyedUnarchiver.unarchivedObject(ofClass: ARWorldMap.self, from: data)
startSession(options: [.resetTracking, .removeExistingAnchors], initialWorldMap: worldMap)
} catch {
fatalError("Can't get worldMap")
}
}
func retrieveWorldMapDataForFilename(_ filename: String) -> Data? {
let url: URL = URL(fileURLWithPath: self.worldMapFilePath(filename))
do {
let data = try Data(contentsOf: url)
return data
} catch {
fatalError("Can't get data at url:\(url)")
}
}
当我尝试使用loadWorldMap() 加载保存的ARWorldMap 时,retrieveWorldMapDataForFilename(_ filename: String) 中的底部致命错误被捕获并出现以下错误
线程 1:致命错误:无法获取数据 url:file:///var/mobile/Containers/Data/Application/924B012B-B149-4BA7-BFC2-BB79849D866F/Documents/WorldMaps/WorldMap_test
我做错了什么?
【问题讨论】:
标签: ios swift arkit nskeyedarchiver nskeyedunarchiver