在 Cocoa/C 中使用相对路径加载资源
我目前第一次直接与 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用 NSBundle
bundleWithIdentifier:
而不是mainBundle
来获取对屏幕保护程序包的引用。来自讨论部分:
You should use NSBundle
bundleWithIdentifier:
instead ofmainBundle
to get a reference to your screen saver bundle.From the discussion section: