无法在软件包中的xcassets中加载图像
我需要将图像包含在静态库中。 我创建了一个包并插入到我的图像中,问题在于,如果将图像直接包含在包中,似乎可以正常工作,但如果放入xcassets文件就会停止工作。无法在软件包中的xcassets中加载图像
我遵循了许多指南并在本网站上搜索解决方案。 最流行的解决方案是插入这行代码:
[UIImage imageNamed:@"MyBundle.bundle/imageName"]
但似乎不适合我
任何工作思路?
有两种方法来解决这个问题,
如果您的应用仍然支持IOS 7,你可以使用这个类别: https://gist.github.com/serluca/e4f6a47ffbc19fccc63e
否则,IOS 8日起苹果增加了一个办法做到这一点使用: + imageNamed:inBundle:compatibleWithTraitCollection:
定义here
运行相同的问题。看起来像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设为零通过主屏幕特性为每apple docs(我不太得到什么意思不过,如果有谁知道请发表评论!)。
任何好办法在iOS7中做到这一点? – user1010819 2014-12-20 12:56:53
所以我一直有同样的问题,看起来像是xcode直接将xcassets编译为二进制文件(.car)并复制到主包中,这意味着xcassets不能包含在捆绑资源中。 https://devforums.apple.com/message/968859#968859 – 2015-02-06 11:49:39
我们的图片放置在Images.xcassets,我们曾与在IBDesignable加载图像的问题。下面的代码确实在界面生成的预览和应用工作,以及:
NSBundle* bundle = [NSBundle bundleForClass:[self class]];
UIImage* image = [UIImage imageNamed:@"image.jpg" inBundle:bundle compatibleWithTraitCollection:nil];
正如我说如果你使用的iOS 7,你可以使用我的类别这种方法可从IOS 8开始,而不是以https://gist.github。 com/serluca/e4f6a47ffbc19fccc63e – Serluca 2015-05-01 13:09:07
对于斯威夫特2。 1:
let bundle = pathToBundle // define for your app or framework
if let image = UIImage(named: "drop_arrow", inBundle: bundle, compatibleWithTraitCollection: nil) {
// process image
}
面临同样的问题,你能解决这个问题吗? – BaSha 2015-02-26 07:07:47
@BaSha有可能使用IOS 8用这种方法: +(的UIImage *)imageNamed:(的NSString *)名称inBundle:(一个NSBundle *)束compatibleWithTraitCollection:(UITraitCollection *)traitCollection; 与iOS 7的最佳解决方案是从xcassets删除图像文件 – Serluca 2015-02-26 09:38:22
感谢,但我不得不在捆绑单独添加图像作为iOS的7支持是必需的 – BaSha 2015-02-26 09:44:58