开发者

Using Relative Paths to Load Resources in Cocoa/C++

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 tr开发者_如何学运维ied out anything that came to my mind without success so I would be glad about some input!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜