Swift iOS ARKit 从文件加载 ARWorldMap 数据 NSKeyedUnarchiver NSKeyedArchiver
Swift iOS ARKit Load ARWorldMap Data from File NSKeyedUnarchiver NSKeyedArchiver
我正在尝试保存然后将 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)
中的底部 fatalError 被捕获并出现以下错误
Thread 1: Fatal error: Can't get data at
url:file:///var/mobile/Containers/Data/Application/924B012B-B149-4BA7-BFC2-BB79849D866F/Documents/WorldMaps/WorldMap_test
我做错了什么?
查看您的代码,问题出在以下函数中:
func worldMapFilePath(_ fileName: String) -> String {}
当您保存世界地图时,此功能工作正常,但当您尝试检索它时,文件被删除。
如果记录实际错误:
fatalError("Error = \(error)")
而不是使用这个:
fatalError("Can't get data at url:\(url)")
您将收到以下错误消息:
"The file “WorldMap_test” couldn’t be opened because there is no such
file.
哪个比您问题中提供的信息更丰富,例如:
Can't get data at
url:file:///var/mobile/Containers/Data/Application/924B012B-B149-4BA7-BFC2-BB79849D866F/Documents/WorldMaps/WorldMap_test
因此一些简单的更改将解决您的问题。
首先,我建议您将电话从 createWorldMapsFolder()
转移到 viewDidLoad
。
然后像这样更改您的 func worldMapFilePath(_ fileName: String) -> String {}
:
func worldMapFilePath(_ fileName: String) -> String {
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = paths[0] as String
let filePath: String = "\(documentsDirectory)/WorldMaps/WorldMap_\(fileName)"
return filePath
}
然后将您的 fileExists
调用移动到您的 saveWorldMap
函数,例如:
func saveWorldMap() {
sceneView.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 worldMapPath = self.worldMapFilePath("test")
if FileManager().fileExists(atPath: worldMapPath) { try! FileManager().removeItem(atPath: worldMapPath) }
let url: URL = URL(fileURLWithPath: worldMapPath)
try data.write(to: url)
} catch {
fatalError("Can't write to url")
}
} catch {
fatalError("Can't encode map")
}
}
}
希望对您有所帮助...
我正在尝试保存然后将 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)
中的底部 fatalError 被捕获并出现以下错误
Thread 1: Fatal error: Can't get data at url:file:///var/mobile/Containers/Data/Application/924B012B-B149-4BA7-BFC2-BB79849D866F/Documents/WorldMaps/WorldMap_test
我做错了什么?
查看您的代码,问题出在以下函数中:
func worldMapFilePath(_ fileName: String) -> String {}
当您保存世界地图时,此功能工作正常,但当您尝试检索它时,文件被删除。
如果记录实际错误:
fatalError("Error = \(error)")
而不是使用这个:
fatalError("Can't get data at url:\(url)")
您将收到以下错误消息:
"The file “WorldMap_test” couldn’t be opened because there is no such file.
哪个比您问题中提供的信息更丰富,例如:
Can't get data at url:file:///var/mobile/Containers/Data/Application/924B012B-B149-4BA7-BFC2-BB79849D866F/Documents/WorldMaps/WorldMap_test
因此一些简单的更改将解决您的问题。
首先,我建议您将电话从 createWorldMapsFolder()
转移到 viewDidLoad
。
然后像这样更改您的 func worldMapFilePath(_ fileName: String) -> String {}
:
func worldMapFilePath(_ fileName: String) -> String {
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = paths[0] as String
let filePath: String = "\(documentsDirectory)/WorldMaps/WorldMap_\(fileName)"
return filePath
}
然后将您的 fileExists
调用移动到您的 saveWorldMap
函数,例如:
func saveWorldMap() {
sceneView.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 worldMapPath = self.worldMapFilePath("test")
if FileManager().fileExists(atPath: worldMapPath) { try! FileManager().removeItem(atPath: worldMapPath) }
let url: URL = URL(fileURLWithPath: worldMapPath)
try data.write(to: url)
} catch {
fatalError("Can't write to url")
}
} catch {
fatalError("Can't encode map")
}
}
}
希望对您有所帮助...