如何开发包含图像资源的iOS框架?

Gao*_*Hsu 6 frameworks objective-c nsbundle ios ios-frameworks

我正在开发一个包含图像资源的iOS框架,我在框架中调用下面的方法,

crossImage = [UIImage imageNamed:@"cross"];
arrowImage = [UIImage imageNamed:@"arrow"];
Run Code Online (Sandbox Code Playgroud)

然后我构建了一个演示来测试我的框架,但是发现crossImage和arrowImage都是零; 之后,我发现imageNamed:方法将搜索应用程序目录中不在框架中的图像,因此我可以通过将两个图像添加到演示项目来修复它.然而,它几乎没有优雅.那么在我的框架中定位图像的任何其他解决方案?

Mid*_* MP 22

您可以使用以下方法从框架加载图像:

+ (UIImage *)imageNamed:(NSString *)name
               inBundle:(NSBundle *)bundle
compatibleWithTraitCollection:(UITraitCollection *)traitCollection
Run Code Online (Sandbox Code Playgroud)

方法.

在你的框架类中写如下:

NSBundle *frameWorkBundle = [NSBundle bundleForClass:[self class]];
UIImage *arrow = [UIImage imageNamed:@"arrow" inBundle:frameWorkBundle compatibleWithTraitCollection:nil];
UIImage *cross = [UIImage imageNamed:@"cross" inBundle:frameWorkBundle compatibleWithTraitCollection:nil];
Run Code Online (Sandbox Code Playgroud)

有关此方法的详细信息,请参阅UIImage类参考.


Gao*_*Hsu 1

最后,我创建了一个包来保存所有图像资源,并且我们提供了 *.framework 和 *.bunlde,这样更优雅。

  • 您能更清楚地说明如何提供这些吗?示例代码会很有帮助。 (3认同)