Chr*_*ick 7 c macos core-foundation nsbundle
是否有用于查找应用程序包中文件路径的C API?
我知道这可以在Objective-C中使用以下语法完成.
NSString *path = [[NSBundle mainBundle] pathForResource:@"MyImage" ofType:@"bmp"];
Run Code Online (Sandbox Code Playgroud)
我是否可以从C或C++代码调用相应的函数?
在Mike K指出正确的方向后,我能够使用以下代码检索应用程序包中文件的路径.
// Get a reference to the main bundle
CFBundleRef mainBundle = CFBundleGetMainBundle();
// Get a reference to the file's URL
CFURLRef imageURL = CFBundleCopyResourceURL(mainBundle, CFSTR("MyImage"), CFSTR("bmp"), NULL);
// Convert the URL reference into a string reference
CFStringRef imagePath = CFURLCopyFileSystemPath(imageURL, kCFURLPOSIXPathStyle);
// Get the system encoding method
CFStringEncoding encodingMethod = CFStringGetSystemEncoding();
// Convert the string reference into a C string
const char *path = CFStringGetCStringPtr(imagePath, encodingMethod);
fprintf(stderr, "File is located at %s\n", path);
Run Code Online (Sandbox Code Playgroud)
这似乎比它需要的时间长一点,但至少它有效!
你可以通过以下方式获得主要包裹:
CFBundleRef mainBundle = CFBundleGetMainBundle();
Run Code Online (Sandbox Code Playgroud)
详情如下:
| 归档时间: |
|
| 查看次数: |
6336 次 |
| 最近记录: |