Asset catalogs and frameworks

Anybody know if there's a way to specify the bundle used for NSImage(named:)? As far as I know, that's the only way into a compiled asset catalog. Right now it looks like my framework is trying to load images from my app's asset catalog, which has different contents. I can access the framework's resources by getting the URL from NSBundle(forClass:) and working from there, but what I really want is the asset catalog.

Actually you can use an Asset Catalog with frameworks. I heard back on that bug report.


You need to use NSBundle's imageForResource: method.


NSBundle *bundle = [NSBundle bundleForClass:[self class]];

NSImage *image = [bundle imageForResource:@"ImageNameInAssetCatalog"];

The only way to get images from asset catalogs in frameworks is by using private APIs.

Was just about to ask the exact same question. UIImage has constructors that take a bundle as a param (not sure if they work with an AC, but I imagine they do), too bad OS X didn't get the same love.


It'd be nice if NSBundle's URLForImageResource: method worked with images in an Asset catalog, but it does not. I tried this:


   NSURL *patternedBackgroundImageURL = [bundle URLForImageResource:@"PatternedBGImage"];
    NSImage *windowBackgroundColorImage = [[NSImage alloc]initWithContentsOfURL:patternedBackgroundImageURL];


Nooo good.


I filed a bug

24510294

Accepted Answer

Actually you can use an Asset Catalog with frameworks. I heard back on that bug report.


You need to use NSBundle's imageForResource: method.


NSBundle *bundle = [NSBundle bundleForClass:[self class]];

NSImage *image = [bundle imageForResource:@"ImageNameInAssetCatalog"];

Aha! Yeah, I also tried using -URLForImageResource: with an equal lack of success. I ended up just moving everything out of the asset catalog and using traditional resources, but you're right: -imageForResource: works!


Oddly enough, I haven't heard a peep about my radar (#24090114), but if I recall correctly, I filed mine as an enhancement request, not a bug. Maybe they pay less attention to enhancement requests.

This is returning nil on OS X 10.9.


Cheers,

Jay

Great solution — thank you.

There is also

-(UIImage*)[UIImage imageNamed:inBundle:compatibleWithTraitCollection:]