Swift iOS ARKit 从文件 NSKeyedUnarchiver NSKeyedArchiver 加载 ARWorldMap 数据
Posted
技术标签:
【中文标题】Swift iOS ARKit 从文件 NSKeyedUnarchiver NSKeyedArchiver 加载 ARWorldMap 数据【英文标题】:Swift iOS ARKit Load ARWorldMap Data from File NSKeyedUnarchiver NSKeyedArchiver 【发布时间】: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
我做错了什么?
【问题讨论】:
【参考方案1】:查看您的代码,问题出在以下函数中:
func worldMapFilePath(_ fileName: String) -> String
此功能在您保存世界地图时工作正常,但当您尝试检索它时,该文件将被删除。
如果您记录实际错误:
fatalError("Error = \(error)")
而不是使用这个:
fatalError("Can't get data at url:\(url)")
您将收到以下错误消息:
“无法打开文件“WorldMap_test”,因为没有这样的文件 文件。
这比您问题中提供的信息更丰富,例如:
无法获取数据 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")
希望对你有帮助...
【讨论】:
以上是关于Swift iOS ARKit 从文件 NSKeyedUnarchiver NSKeyedArchiver 加载 ARWorldMap 数据的主要内容,如果未能解决你的问题,请参考以下文章