gpt4 book ai didi

cocoa - 创建 Cocoapod 库和 [UIImage imageNamed :] returns NULL

转载 作者:行者123 更新时间:2023-12-03 16:02:45 29 4
gpt4 key购买 nike

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

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

s.resource_bundles = {
'FDChessboardView' => ['Pod/Assets/*']
}

(我还尝试将这些文件直接添加到 XCode 内的 Development Pods/FDChessboardView/Resources 组中。)

在库实现文件中,我需要引用这些图像。我尝试过:

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

此处 imagePath 设置正确。该文件存在,并且 file 确认它是 PNG:

[...]aa.png: PNG image data, 182 x 164, 8-bit/color RGBA, non-interlaced

但是 UIImage 为 NULL。

我也尝试过这些:

image = [UIImage imageNamed:@"aa"];
UIImage *image = [UIImage imageNamed:@"FDChessboardView.bundle/aa.png"];
image = [UIImage imageNamed:@"aa" inBundle:bundle compatibleWithTraitCollection:nil];

它们都产生 NULL。

有人可以帮我指出如何加载主题图像资源的正确方向吗?

谢谢!

最佳答案

好的 - 我找到了适合我的解决方案:

在我创建的库中,我有以下类: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];

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

关于cocoa - 创建 Cocoapod 库和 [UIImage imageNamed :] returns NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25835862/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com