是否有 C API 用于查找应用程序包中文件的路径?
我知道这可以在 Objective-C 中使用以下语法来完成。
NSString *path = [[NSBundle mainBundle] pathForResource:@"MyImage" ofType:@"bmp"];
是否也可以从 C 或 C++ 代码调用相应的函数?
After 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);
这似乎比需要的时间长一点,但至少它有效!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)