您的位置:首页 > 其它

Impossible to load an image in xcassets on bundle

2016-04-07 15:08 489 查看

Impossible to load an image in xcassets on bundle






up vote5down votefavorite
3

I need to include images in a static library. I created a bundle and inserted in my images, the problem is that it seems to work if I include the images directly in the bundle, but stops working if I put in a xcassets file.

I followed many guides and searched for a solution on this site. The most popular solution is to insert this line of code:

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

but it seems not work for me

any ideas?

ios static-libraries nsbundle xcasset
shareeditflag
asked Oct 2 '14 at 10:30



Serluca
1,5851920

facing same issue, Did you able to fix this? – BaSha Feb 26 '15 at 7:07
@BaSha it is possible using iOs 8 with this method: + (UIImage *)imageNamed:(NSString *)name inBundle:(NSBundle *)bundle compatibleWithTraitCollection:(UITraitCollection *)traitCollection; With iOs 7 the best solution is remove the images from the xcassets file – Serluca Feb 26 '15 at 9:38
thanks, though I had to add images separately in bundle as iOS 7 support was required – BaSha Feb 26 '15 at 9:44
2
@BaSha I created this category gist.github.com/serluca/e4f6a47ffbc19fccc63e . In this way, you can use after: [NSBundle imageNamed:@"imageName"]; – Serluca Feb 26 '15 at 10:20
add a comment

4 Answers

activeoldestvotes

up vote7down voteaccepted
There are two ways to solve this,

If your app is still supporting iOs 7, you can use this category:https://gist.github.com/serluca/e4f6a47ffbc19fccc63e

Otherwise, starting from iOs 8 Apple added a way to do this using:
+ imageNamed:inBundle:compatibleWithTraitCollection:
defined here

shareeditflag
answered May 1 '15 at 13:13



Serluca
1,5851920

know of any way to refer to this inside Interface Builder? – jowie Dec 3 '15 at 11:09
1
@jowie did you try with this stackoverflow.com/a/7733614/1728552Serluca Dec 3 '15 at 12:38
I did but that refers to a png, rather than an .xcassets identifier. Having said that I didn't realise but my problem goes a little deeper because it's not importing assets from my custom framework. I will try it again after (hopefully) I out out my other issue. Thanks! – jowie Dec 3 '15 at 15:05
imageNamed:inBundle:compatibleWithTraitCollection: works with a PNG embedded in the bundle flat, but once you put it in the .xcassets folder, I cannot find a way of referring to it anymore... – jowie Dec 3 '15 at 15:35
add a comment





up vote5down vote
Running the same problem. Looks like inline bundle support is broken for XCAssets in the 1-parameter imageNamed method. There's a workaround though using imageNamed:inBundle:compatibleWithTraitCollection: Be careful, this is iOS8 only !!

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];

NOTE : traitCollection is set to nil to pass the main screen traits as per apple docs (i don't quite get what it means though, if anyone knows please comment!).

shareeditflag
edited Oct 22 '14 at 16:23

answered Oct 22 '14 at 13:47



vivien.destpern
2581210

any good way to do it in iOS7? – user1010819 Dec 20 '14 at 12:56
2
So I've been having the same issue, looks like the scenario is that xcode compile xcassets directly to a binary file (.car) and copies into main bundle, which mean xcassets cannot be contained in a bundle resource. devforums.apple.com/message/968859#968859Adil Soomro Feb 6 '15 at 11:49
add a comment
up vote1down vote
Our images are placed in Images.xcassets and we had a problem with loading images in an IBDesignable. The following code did the job for the preview in Interface builder and the app as well:

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


shareeditflag
answered May 1 '15 at 11:30



user1128713
16117

As I said this method is available on starting from iOs 8. If you use iOs 7 you can use my category insteadgist.github.com/serluca/e4f6a47ffbc19fccc63eSerluca May 1 '15 at 13:09
add a comment
up vote1down vote
For 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
}


shareeditflag
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: