ios - How to transfer resource files with Static Library (How to wrap resources in bundle)?

标签 ios resources bundle static-libraries nsbundle


我正在为 iOS 应用程序创建一个静态库。我快完成了,但是资源有问题。

我的静态库使用了大量图像和声音文件。如何将它与我的静态库一起传输?

我知道我们可以将其打包并与 .a 文件一起提供。但我不知道如何将图像和声音文件包装在 Bundle 文件中。

我做了什么:

  • 我搜索了很多,但找不到任何有用的链接。
  • 我得到了 Conceptual CFBundles引用,但没有找到解决我的问题的方法。
  • 我检查了可用于 XCode 的文件模板,但没有看到除 Settings Bundle 之外的任何包类型。

最佳答案

有几个很好的理由来构建具有多个 bundle 的应用程序和几种不同的方法。根据我的经验,最好的方法是打开 Xcode 并创建一个新的 bundle 项目:

  1. 选择:文件 -> 新项目... -> Mac OSX (!) -> Framework & Library -> Bundle。将您的资源文件添加到项目中。
  2. 在构建其他 iPhone 应用程序时构建 bundle 。
  3. 您可以将这个项目添加到您的静态库项目中,并在库发生变化时重新构建它。您必须知道 bundle 本身不会链接到您的库文件。
  4. 在您的应用程序项目中,将 .bundle 文件作为普通资源文件添加到您的项目中(添加 -> 现有文件... -> 找到并选择上面构建的 .bundle 文件。不要复制它)。

示例:

// Static library code:
#define MYBUNDLE_NAME       @"MyResources.bundle"   
#define MYBUNDLE_IDENTIFIER @"eu.oaktree-ce.MyResources"
#define MYBUNDLE_PATH       [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: MYBUNDLE_NAME]
#define MYBUNDLE            [NSBundle bundleWithPath: MYBUNDLE_PATH]

// Get an image file from your "static library" bundle:

- (NSString *) getMyBundlePathFor: (NSString *) filename
{
        NSBundle *libBundle = MYBUNDLE;
        if( libBundle && filename ){
            return [[libBundle resourcePath] stringByAppendingPathComponent: filename];
        }
        return nil;
}

// .....
// Get an image file named info.png from the custom bundle
UIImage *imageFromMyBundle = [UIImage imageWithContentsOfFile: [self getMyBundlePathFor: @"info.png"] ];

更多帮助你可以查看这些好文章

  1. iOS Library With Resources

  2. Resource Bundles

希望对你有帮助。

关于ios - How to transfer resource files with Static Library (How to wrap resources in bundle)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16297696/

相关文章:

iphone - 使用 CLLocationManager 的正确方法(开始/停止更新用户位置)

ios - 从存储在 32/64 位 NSNumber 中的 unix 时间戳创建 NSDate

c# - wpf中的图像问题(图像不显示)

linux - Linux 驱动程序中的 platform_get_resource 是什么?

python - 如何在纯 Python 应用程序包中更改 OSX 菜单栏中的应用程序名称?

css - Textmate "comment"命令不能为 css 代码正常工作

symfony - 同 bundle 不同路线

ios - Swift 字符串附加可选的 nil

ios - 如何在 Xcode 6 上对应用程序扩展进行单元测试

java - 在哪里存储用于单元测试的二进制文件