访问mac osx应用程序包中的资源文件夹中的文件 [英] Accessing files in resources folder in mac osx app bundle

查看:242
本文介绍了访问mac osx应用程序包中的资源文件夹中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要访问应用程序包中资源内的文件。不幸的是我不能使用QT resorces,因为我使用CascadeClassifier从opencv。我当前的路径是

I would like to access files which are inside Resources in app bundle. Unfortunately i cannot use QT resorces, as i'm using CascadeClassifier from opencv. My current paths are

const std::string FACE_CLASIFIER_PATH = "/Resources/haarcascade_frontalface_default.xml";
const std::string EYES_CLASIFIER_PATH = "/Resources/haarcascade_mcs_eyepair_big.xml";

我也尝试过

const std::string FACE_CLASIFIER_PATH = "../Resources/haarcascade_frontalface_default.xml";
const std::string EYES_CLASIFIER_PATH = "../Resources/haarcascade_mcs_eyepair_big.xml";

但是其中的一些工作。至于config两个文件都存在于 MyApp.app/Contents/Resources 内,我使用qmake包含它们

But nether of them work. As for config both files are present inside MyApp.app/Contents/Resources, i include them using qmake

mac {
APP_XML_FILES.files = ../haarcascade_frontalface_default.xml ../haarcascade_mcs_eyepair_big.xml
APP_XML_FILES.path = Contents/Resources
QMAKE_BUNDLE_DATA += APP_XML_FILES
}

我非常感谢您对此问题的任何帮助

I would appreciate any help with this issue

推荐答案

你不说你想做的文件,虽然这可能是由于我缺乏opencv的知识。但是,您可以使用Core Foundation类来获取resources文件夹中的文件路径: -

You don't say what you want to do with the files, though that could be due to my lack of knowledge of opencv. However you can use the Core Foundation classes to get paths to files in the resources folder: -

CFURLRef appUrlRef;
appUrlRef = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("somefile"), NULL, NULL);

// do something with the file
//...

// Ensure you release the reference
CFRelease(appUrlRef);

使用CFURLRef,您可以使用 Apple的文档来获取您需要的内容。

With a CFURLRef, you can use Apple's documentation to get what you need from it.

例如,如果你想要一个文件路径: -

For example, if you want a file path: -

CFStringRef filePathRef = CFURLCopyPath(appUrlRef);

//始终释放使用create或copy e
CFRelease(filePathRef);

// Always release items retrieved with a function that has "create or "copy" in its name CFRelease(filePathRef);

从文件路径,我们可以得到一个char *到路径: -

From the file path, we can get a char* to the path: -

const char* filePath = CFStringGetCStringPtr(filePathRef, kCFStringEncodingUTF8);

所以,把它放在一起,如果你想得到一个char *路径haarcascade_mcs_eyepair_big.xml: -

So, putting it all together, if you want to get a char* path to haarcascade_mcs_eyepair_big.xml: -

CFURLRef appUrlRef = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("haarcascade_mcs_eyepair_big.xml"), NULL, NULL);
CFStringRef filePathRef = CFURLCopyPath(appUrlRef);
const char* filePath = CFStringGetCStringPtr(filePathRef, kCFStringEncodingUTF8);

// Release references
CFRelease(filePathRef);
CFRelease(appUrlRef);

这篇关于访问mac osx应用程序包中的资源文件夹中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆