Impossible to load an image in xcassets on bundle


Impossible to load an image in xcassets on bundle

favorite 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
activeoldestvotes 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/1728552 – Serluca 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
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#968859 – Adil Soomro Feb 6 '15 at 11:49
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/e4f6a47ffbc19fccc63e – Serluca May 1 '15 at 13:09
shareeditflag