6

We're getting these kind of error messages:

Could not load the "iconStatus" image referenced from a nib in the bundle with identifier "com.company.OurResourceBundle".

Basically, we put a bunch of images in the xcassets folder ( which works for non-bundle loaded cases ). The xcassets and nib files are packed into a resource bundle.

When the app launches,

  1. uiimageview in the nib file cannot load any images with the above error message.
  2. [UIImage imageNamed:@"OurResourceBundle.bundle/iconStatus"] returns nil

The question is related to "How can I load an image from Assets.car (compiled version of xcassets) within an NSBundle?", but we don't use CocoaPods.

Community
  • 1
  • 1
Tom Fishman
  • 1,716
  • 6
  • 22
  • 36
  • The answer is going to be essentially the same as for the question you reference. imageNamed: only loads from the main bundle, so you'll have to create a category that does all the right things to load your images from a different bundle. – David Berry Apr 04 '14 at 18:06
  • I tried and it doesn't work. There is something special for xcassets being Bundled – Tom Fishman Apr 05 '14 at 00:53
  • How do you get the compiled `*.car` file? From my short experience all `*.xcassets` images just get copied (with a proper name) flat inside the `*.app` directory. – Rivera Apr 07 '14 at 00:18
  • I added the .xcassets (a folder in the harddrive) to the "Copy Bundle Resource" section for the Bundle target of the static library. Xcode build the .car file automatically in the bundle. – Tom Fishman Apr 07 '14 at 01:18
  • This link might help : http://stackoverflow.com/questions/10998544/uiimage-imagenamed-returns-nil Have you try something like:- [UIImage imageNamed:@"iconStatus"]; or [UIImage imageNamed:@"iconStatus.png"]; – Ricky May 05 '14 at 10:10
  • I didn't know about the a compiled xcasset. In my experience images just get properly named and copied to the root of the main bundle (the root `.app` folder). – Rivera May 09 '14 at 06:04
  • If you set your project's `iOS Deployment Target` to iOS versions earlier than 7.0, the `xcassets` file will not be compiled to `.car`. I tested it and if it doesn't hurt for you to change your `iOS Deployment Target` to iOS 6, this might be a solution. – Ge Liu Dec 23 '15 at 15:06

3 Answers3

9

In iOS 7 it is not possible to load images from any .car file aside from the one that Xcode compiles into the main bundle. This was confirmed by an Apple Developer at https://devforums.apple.com/message/968859#968859:

Unfortunately it is not possible to load images from any car file aside from the one that Xcode compiles into your main bundle, as +imageNamed: does not accept a bundle parameter, which is what is needed to do so (and even then it would only be able to open a single asset catalog in a single bundle).

In iOS 8 there is a new method that appears to do what you want:

+ (UIImage *)imageNamed:(NSString *)name inBundle:(NSBundle *)bundle
    compatibleWithTraitCollection:(UITraitCollection *)traitCollection
torrey.lyons
  • 5,519
  • 1
  • 23
  • 30
  • 3
    It doesn't work for me. Seems like that method only reads the image directly under the bundle, but not the image in xcassets inside that bundle :( – Hlung Mar 27 '15 at 10:10
  • I cant find solution for this anywhere, should i just make all images as separate file from xcasset? – Tj3n Sep 29 '16 at 08:31
  • That is what I ended up doing. It is a pain not being able to use .xcasset files in bundles, but it works fine if you just include your images in the bundle the old fashioned way. – torrey.lyons Sep 30 '16 at 00:42
  • But then i will miss the `@2x` and `@3x` feature is that right? If i do that i should just remove `@1x` and `@2x` and keep the largest one? In IOS8 it's possible but my framework is suppose to support from ios 7 thats why – Tj3n Oct 04 '16 at 02:57
  • You can provide '@2x' and '@3x' versions of the images. The only restriction is that they need to be named according to the naming conventions and then they will be used appropriately. For example: image.png, image@2x.png, and image@3x.png. (No '@1x' for the standard resolution image) – torrey.lyons Oct 04 '16 at 04:40
0

you have to define your bundle

  imageView.image = UIImage(named: "iconName", in: Bundle.main, compatibleWith: nil)
Abuzeid
  • 962
  • 10
  • 14
-1

I suspect if your image assets folder gets compressed into a .car file then NSBundle will know how to search for assets inside of it. Try creating a new bundle from your resources bundle. Then ask that bundle for the path of your image. Finally create an image from that path. This is sort of the approach we take when bundling image assets and localized strings with our static libraries. Our version is a little more involved as it takes care of things like @2x and ~ipad images automatically but this is the gist of it.

    NSBundle *myBundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"your bundle" ofType:@"bundle"]];
    NSString *filePath = [myBundle pathForResource:@"icon" ofType:@"png"];
    UIImage *image = [UIImage imageWithContentsOfFile:filePath];
Brandon
  • 2,387
  • 13
  • 25