不可能在软件包的xcassets中加载图像

我需要在静态库中包含图像。 我创build了一个包,并插入到我的图像,问题是,如果我直接在包中包含图像似乎工作,但停止工作,如果我把一个xcassets文件。

我遵循了许多指南,并在本网站上search解决scheme。 最stream行的解决scheme是插入这一行代码:

[UIImage imageNamed:@"MyBundle.bundle/imageName"] 

但似乎不适合我

有任何想法吗?

有两种方法可以解决这个问题,

如果您的应用程序仍支持iOs 7,则可以使用以下类别: https : //gist.github.com/serluca/e4f6a47ffbc19fccc63e

否则,从iOs 8开始,苹果添加了一个方法来使用: + imageNamed:inBundle:compatibleWithTraitCollection:定义在这里

运行相同的问题。 对于1参数imageNamed方法中的XCAssets,内联包支持看起来已经损坏。 有一个解决方法,虽然使用imageNamed:inBundle:compatibleWithTraitCollection:小心,这只是iOS8!

 NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"static_lib_bundle_name" ofType:@"bundle"]]; UIImage *image = [UIImage imageNamed:@"image_in_the_xcassets_you_want" inBundle:bundle compatibleWithTraitCollection:nil]; 

注意:traitCollection被设置为nil来传递主屏幕的特征,如苹果文档 (我不完全明白它的意思,但如果有人知道请评论!)。

我们的图像被放置在Images.xcassets中,并且在IBDesignable中加载图像时遇到了问题。 以下代码完成了“界面”构build器和应用程序中的预览工作:

 NSBundle* bundle = [NSBundle bundleForClass:[self class]]; UIImage* image = [UIImage imageNamed:@"image.jpg" inBundle:bundle compatibleWithTraitCollection:nil]; 

对于Swift 2.1:

 let bundle = pathToBundle // define for your app or framework if let image = UIImage(named: "drop_arrow", inBundle: bundle, compatibleWithTraitCollection: nil) { // process image }