【问题标题】:Is ModelIO/MDLAsset myopic?ModelIO/MDLAsset 是短视的吗?
【发布时间】: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 中删除了沙箱,它一切正常。非常感谢。

标签: swift metalkit modelio


【解决方案1】:

该问题并非特定于 ModelIO 或 MDLAsset;这是沙盒应用程序的普遍问题。沙盒应用无法访问任意用户文件,它只能访问自己沙盒中的文件,除非用户交互已授予它访问其他文件的权限。

例如,如果您的应用要使用文件打开对话框 (NSOpenPanel) 要求用户选择模型对象文件,并且用户要这样做,那么您的应用将获得对该文件的访问权限.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-06
    • 1970-01-01
    • 2013-12-18
    • 1970-01-01
    • 2011-01-05
    • 2019-01-05
    相关资源
    最近更新 更多