11

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?

Serluca
  • 2,092
  • 2
  • 17
  • 29
  • facing same issue, Did you able to fix this? – BaSha Feb 26 '15 at 07: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 09:38
  • thanks, though I had to add images separately in bundle as iOS 7 support was required – BaSha Feb 26 '15 at 09:44
  • 2
    @BaSha I created this category https://gist.github.com/serluca/e4f6a47ffbc19fccc63e . In this way, you can use after: [NSBundle imageNamed:@"imageName"]; – Serluca Feb 26 '15 at 10:20

4 Answers4

10

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

Serluca
  • 2,092
  • 2
  • 17
  • 29
  • 1
    know of any way to refer to this inside Interface Builder? – jowie Dec 03 '15 at 11:09
  • 1
    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 03 '15 at 15:05
  • 4
    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 03 '15 at 15:35
8

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!).

vivien.destpern
  • 950
  • 1
  • 6
  • 14
  • 3
    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. https://devforums.apple.com/message/968859#968859 – Adil Soomro Feb 06 '15 at 11:49
3

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
}
vmanjz
  • 1,046
  • 1
  • 9
  • 21
2

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];
Paul Schröder
  • 1,138
  • 8
  • 19
  • As I said this method is available on starting from iOs 8. If you use iOs 7 you can use my category instead https://gist.github.com/serluca/e4f6a47ffbc19fccc63e – Serluca May 01 '15 at 13:09