Accessing files in resources folder in mac osx app bundle

前端 未结 1 539
被撕碎了的回忆
被撕碎了的回忆 2020-12-21 14:33

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

1条回答
  •  隐瞒了意图╮
    2020-12-21 14:59

    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);
    

    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);
    

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

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

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

    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);
    

    0 讨论(0)
提交回复
热议问题