【发布时间】:2019-12-31 03:25:54
【问题描述】:
使用Swift/MDLAsset 加载“.obj”的常用方法是使用类似代码
import ModelIO
var theURL: URL
var theAsset: MDLAsset
theURL = Bundle.main.url(forResource: "cube", withExtension: "obj")!
theAsset = MDLAsset(url: theURL)
这仅适用于应用主 bundle(在 app/Contents/Resources 上的 macOS)中的文件。
但我希望我的应用程序能够从文件系统上的任何位置读取文件。所以我尝试了以下
// 1st attempt
theURL = URL(string: "file:///Users/me/cube.obj")!
theAsset = MDLAsset(url: theURL)
// 2nd attempt
theURL = URL(fileURLWithPath: "/Users/me/cube.obj")
theAsset = MDLAsset(url: theURL)
// 3rd attempt
theURL = URL(string: "cube.obj", relativeTo: URL(string:"/Users/me/")!)!
theAsset = MDLAsset(url: theURL)
他们都失败了(错误消息"Could not open OBJ file")。仅当"cube.obj" 文件不在app/Contents/Resources 下时才会发生这种情况。
我的幼稚结论是 MDLAsset 似乎是短视的——它只在一个地方看:app/Contents/Resources.
我确信必须有一个解决方案(除了总是将我的 obj 文件复制到应用程序的资源中)。
【问题讨论】:
-
你的应用是沙盒的吗?
-
嗨,肯,就是这样!我从 Xcode/Signing 和 Capabilities 中删除了沙箱,它一切正常。非常感谢。