ios - 如何开发包含图片资源的iOS框架?

标签 ios objective-c frameworks nsbundle ios-frameworks

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

crossImage = [UIImage imageNamed:@"cross"];
arrowImage = [UIImage imageNamed:@"arrow"];

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

最佳答案

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

+ (UIImage *)imageNamed:(NSString *)name
               inBundle:(NSBundle *)bundle
compatibleWithTraitCollection:(UITraitCollection *)traitCollection

方法。

在你的框架类中这样写:

NSBundle *frameWorkBundle = [NSBundle bundleForClass:[self class]];
UIImage *arrow = [UIImage imageNamed:@"arrow" inBundle:frameWorkBundle compatibleWithTraitCollection:nil];
UIImage *cross = [UIImage imageNamed:@"cross" inBundle:frameWorkBundle compatibleWithTraitCollection:nil];

引用UIImage Class Reference有关此方法的更多信息。

关于ios - 如何开发包含图片资源的iOS框架?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28777682/

相关文章:

ios - 限制或扩展播放的样本数量

ios - 通过 SLRequest 代表用户喜欢 Facebook 页面?

ios - 重启后重新连接外设 - Objective-C

java - Spring、Struts、Hibernate、JavaServer Faces、Tapestry 有什么区别?

ios - iOS8.0(和 8.1)上第二个 UIScreen 的自动旋转问题

android - crashlytics 是否有公共(public) API?

iphone - 如何向 UINavigationController 添加右键?

objective-c - 渲染 QCRenderer 以显示在 DeckLink 卡上

Swift 框架 "Module ' 找不到 RealityKit”

swift - Swift编译器错误: During the test the app in the iphone device