繁体   English   中英

如何使用静态库传输资源文件(如何在bundle中包装资源)?

[英]How to transfer resource files with Static Library (How to wrap resources in bundle)?


我正在为iOS应用程序创建一个Static Library 我几乎完成了它,但是资源存在问题。

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

我知道我们可以将它包装在一个包上并沿着.a文件提供它。 但我不知道如何将图像和声音文件包装在Bundle文件中。

我做了什么:

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

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

  1. 选择:文件 - >新建项目... - >组Mac OSX(!) - >框架和库 - >捆绑。 将资源文件添加到项目中。
  2. 在构建其他iPhone应用程序时构建捆绑包。
  3. 您可以将此项目添加到静态库项目中,并在更改库时重建它。 您必须知道捆绑包本身不会链接到您的库文件。
  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库

  2. 资源包

希望它能帮到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM