使用Swift/MDLAsset加载".obj“的常用方法是使用如下代码
import ModelIO
var theURL: URL
var theAsset: MDLAsset
theURL = Bundle.main.url(forResource: "cube", withExtension: "obj")!
theAsset = MDLAsset(url: theURL)这只适用于应用程序的主bundle中的文件(在macOS上的app/Contents/Resource中)。但我希望我的应用程序能够从我的文件系统中的任何位置读取文件。因此,我尝试了以下方法
// 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")。仅当app/Contents/Resources下不存在"cube.obj"文件时,才会发生这种情况。
我天真的结论是,MDLAsset似乎是短视的--它只看一个地方:app/Contents/Resources.
我确信一定有解决方案(除了总是将我的obj文件复制到应用程序的资源中)。
发布于 2020-01-01 13:03:46
这个问题并不是ModelIO或MDLAsset所特有的;这是沙盒应用程序的一个普遍问题。沙箱应用程序不能访问任意用户文件,它只能访问自己沙箱中的文件,除非用户交互授予它访问其他文件的权限。
例如,如果您的应用程序使用文件打开对话框(NSOpenPanel)要求用户选择模型对象文件,并且用户要这样做,那么您的应用程序将获得对该文件的访问权限。
https://stackoverflow.com/questions/59539225
复制相似问题