I am working on a Yosemite application that includes a notification center widget (or Today extension). I have a structure where I store all my common codes and resources, which include some images. I just use PNG or the asset catalog, they do not load if I just use the standard NSImage imageNamed: method. Looking at the documentation, which is clearly expected - it only looks in the main package.
On iOS, UIImage has a new imageNamed:inBundle:compatibleWithTraitCollection: method, which is suitable for this problem. I do not see anything like this for NSImage .
It seems like the best option is to do something like this:
[[NSBundle bundleForClass:[self class]] imageForResource:@"name"];
The main problem is that it does not use caching. To do this, I added my own method to the category:
+ (NSImage *)imageNamed:(NSString *)name inBundle:(NSBundle *)bundle { NSImage *image = [NSImage imageNamed:name]; if (image == nil) { image = [bundle imageForResource:name]; [image setName:name]; } return image; }
It looks good, but is there a better solution?
source share