创建Cocoapod库和[UIImage imageNamed:]返回NULL

Wil*_*ken 15 cocoa cocoa-touch cocoapods xcode6 ios8

我有https://github.com/fulldecent/FDChessBoardView工作得很好,我现在再次从头开始创建项目pod lib create,以便制作Podspec.

有一个{'.h','.m'}文件和一些图像.图像位于提供的Pod/Assets文件夹中的文件系统中.podspec文件中记录了资源:

s.resource_bundles = {
  'FDChessboardView' => ['Pod/Assets/*']
}
Run Code Online (Sandbox Code Playgroud)

(我也试过将这些文件直接添加到Development Pods/FDChessboardView/ResourcesXCode中的组中.)

在库实现文件中,我需要参考这些图像.我试过了:

NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"FDChessboardView" ofType:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
NSString *imagePath = [bundle pathForResource:@"aa" ofType:@"png"];
UIImage* image = [UIImage imageWithContentsOfFile:imagePath];
Run Code Online (Sandbox Code Playgroud)

这里正确设置了imagePath.此文件存在并file确认它是PNG:

[...] aa.png:PNG图像数据,182 x 164,8位/彩色RGBA,非隔行扫描

但是UIImage是NULL.

我也试过这些:

image = [UIImage imageNamed:@"aa"];
UIImage *image = [UIImage imageNamed:@"FDChessboardView.bundle/aa.png"];
image = [UIImage imageNamed:@"aa" inBundle:bundle compatibleWithTraitCollection:nil];
Run Code Online (Sandbox Code Playgroud)

所有这些都产生NULL.

任何人都可以帮我指出如何加载主题图像资产的正确方向?

谢谢!

Rob*_*Rob 11

好的 - 我发现了一个适合我的解决方案:

在我正在创建的lib中,我有以下类: AEBubbleField.h AEBubbleView.m

Assets在生成的文件结构的文件夹中也有一个图像background.png.我想在上面的lib文件中使用它,所以我将以下内容添加到pod规范中(以确保使用我的lib类复制png)

s.ios.resource_bundle = { 'AEBubbleField' => 'Pod/Assets/*.png' }

然后我可以通过以下方式访问图像:

NSBundle *bundle = [NSBundle bundleForClass:[self class]]; // Any class in the lib can be used here, I just went for the current class
UIImage *backgroundImage = [UIImage imageNamed:@"background.png" inBundle:bundle compatibleWithTraitCollection:nil];
Run Code Online (Sandbox Code Playgroud)

这是我获取图像并使用它的唯一方法.我试过的所有其他方法都nil像原始问题一样返回.