8


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

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

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

我做了什么:

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

1 回答 1

9

使用多个捆绑包和几种不同的方法来构建应用程序有几个很好的理由。根据我的经验,最好的方法是打开 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. 资源包

希望它可以帮助你。

于 2013-04-30T10:40:51.810 回答