Load images from Assets.car in a resource nsbundle?

I'm trying to create a plain resource NSBundle that I can load in my iOS app that has some image assets, maybe some tweaked storyboard/nib files, and other data. It doesn't have anything executable in it, (therefore it can't be "loaded"), but I want to extract resources from it.


However, when I use actool to create an Assets.car into the bundle, I cannot extract images from it, even using the new UIImage method imageNamed:inBundle:compatibleWithTraitCollection:


// This should work, but just returns nil.
[UIImage imageNamed:@"bar" inBundle:bundle compatibleWithTraitCollection:nil];


My bundle file content structure is basically:


Foo.bundle/

  • Assets.car
  • Info.plist
  • Resources/
    • green.storyboardc


However, if I use raw images instead of compiling them into a .car (e.g. copy directly, or set actool's "--minimum-deployment-target" to "6.0"), then the file structure looks like:


Foo.bundle/

  • bar.png
  • bar@2x.png
  • bar@3x.png
  • Info.plist
  • Resources/
    • green.storyboardc


And it works, but I lose all slicing info, and other control with device-specific traits and templates, etc. Is there something I'm missing? This post by Rincewind from the old dev forums was written before the imageNamed:inBundle:compatibleWithTraitCollection: was released, but is the closest thing to say this is not possible.

[UIImage imageNamed:@"bar" inBundle:bundle compatibleWithTraitCollection:nil];


Try adding '.png to 'bar' in that line to at least see if that image works as needed - seems similar to this 2013 SO thread...


http://stackoverflow.com/questions/15866180/ios-uiimage-imagenamed-returns-null-on-device


◅▻

Did you get closer to a solution to this? I'm having the same issue as you, with imageNamed:inBundle:compatibleWithTraitCollection: returning nil for my custom bundle.

As a reference to future developers looking for answers. I submitted a Radar, and apple responded with this bahaving as intended. See https://openradar.appspot.com/radar?id=5026213026332672

Thank you alleus, it's helped me!

Hi Riz,


Could you elaborate how you got the assets from the Asset catalog to be copied directly as png files instead of being compiled into the .car file? I tried using the 'Copy Bundle Resources' option in the Build Phases of my target, but that only lets me select the .xcassets file, not the individual pngs contained in it. And that results in the Assets.car file being produced.


Thanks!

Load images from Assets.car in a resource nsbundle?
 
 
Q