在 Cocoa/C 中使用相对路径加载资源

发布于 2024-08-31 12:52:24 字数 828 浏览 8 评论 0原文

我目前第一次直接与 Cocoa 合作构建屏幕保护程序。现在,我在尝试从 .saver 包中加载资源时遇到了问题。我基本上有一个小的 C++ 包装类来使用 freeImage 加载 .exr 文件。只要我使用绝对路径,这就可以工作,但这不是很有用,不是吗?因此,基本上,我尝试了一切:将 .exr 文件放在 .saver 捆绑包本身的级别、捆绑包的 Resources 文件夹中,等等。

然后我只是尝试像这样加载 .exr ,但没有成功:

particleTex = [self loadExrTexture:@"ball.exr"];

我还尝试将其转到 .saver 捆绑包位置,如下所示:

particleTex = [self loadExrTexture:@"../../../ball.exr"];

...也许从该位置加载 .exr,但没有成功。

然后我遇到了这个:

NSString * path = [[NSBundle mainBundle] pathForResource:@"ball" ofType:@"exr"];
const char * pChar = [path UTF8String];

...这似乎是在 Cocoa 中查找资源的常见方法,但由于某种原因它在我的情况下是空的。对此有什么想法吗? 我真的尝试了我想到的任何东西但没有成功,所以我很高兴能得到一些意见!

I am currently working directly with Cocoa for the first time to built a screen saver. Now I came across a problem when trying to load resources from within the .saver bundle. I basically have a small C++ wrapper class to load .exr files using freeImage. This works as long as I use absoulte paths, but that's not very useful, is it? So, basically, I tried everything: putting the .exr file at the level of the .saver bundle itself, inside the bundles Resources folder, and so on.

Then I simply tried to load the .exr like this, but without success:

particleTex = [self loadExrTexture:@"ball.exr"];

I also tried making it go to the .saver bundles location like this:

particleTex = [self loadExrTexture:@"../../../ball.exr"];

...to maybe load the .exr from that location, but without success.

I then came across this:

NSString * path = [[NSBundle mainBundle] pathForResource:@"ball" ofType:@"exr"];
const char * pChar = [path UTF8String];

...which seems to be a common way to find resources in Cocoa, but for some reason it's empty in my case. Any ideas about that?
I really tried out anything that came to my mind without success so I would be glad about some input!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

燃情 2024-09-07 12:52:24

您应该使用 NSBundle bundleWithIdentifier: 而不是 mainBundle 来获取对屏幕保护程序包的引用。

来自讨论部分:

此方法通常用于
框架和插件来定位
他们在运行时自己的包。这
方法可能会更有效一些
而不是尝试使用来定位捆绑包
BundleForClass: 方法。

You should use NSBundle bundleWithIdentifier: instead of mainBundle to get a reference to your screen saver bundle.

From the discussion section:

This method is typically used by
frameworks and plug-ins to locate
their own bundle at runtime. This
method may be somewhat more efficient
than trying to locate the bundle using
the bundleForClass: method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文