我正在开发一个包含图像资源的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/