How can I load an NSImage representation of the icon for my application?

e.James picture e.James · Aug 31, 2009 · Viewed 8.5k times · Source

I have a custom icon file (MyApp.icns) set up for my Cocoa App. How can I access an NSImage representation of the icon from within my application?

Something like the following would be perfect:

NSImage * iconImage = [MyApplication defaultIconAsImage];

But I'm sure it isn't that easy :)

I can, of course, get a path to the icon file as follows:

NSString * iconPath = [[NSBundle mainBundle]
                       pathForResource:@"MyApp" ofType:@"icns"];

But it seems to me that there should be some kind of standard way to access the icon file for the application, other than calling it by name, since the name could change.

What is the proper way to do this?

Answer