What is the best way to upload an image from a framework in OS X Yosemite (10.10)?

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?

+6
source share
1 answer

Currently, the API does not exist for this in one method call, but I just filed the radar: // 25632204 to request that +[NSImage imageNamed:inBundle:] added for parity using UIKit.

+2
source

Source: https://habr.com/ru/post/973247/


All Articles