Gubatron
Gubatron

Reputation: 6479

How to load an image from "Images.xcassets" into an NSImage*?

Not sure how else to specify the path to the image.

I've added a png into a new image named "StatusBarIcon", which lives inside "Images.xcassets" resource folder.

I see they put a json file and what not.

Tried to do what the examples out there say, but no matter what I try, I always get nil as the output of [NSImage imageNamed:@"name of paths attempted here"]

Won't load using the full path either, I suppose the method is looking for the name of the image, but I've no idea how to specify the name correctly it seems, or perhaps I need another method?

Here are screenshots of my code, and my project. different attempts and log output showing it's always nil what my image is like

Upvotes: 8

Views: 6079

Answers (2)

Wil Shipley
Wil Shipley

Reputation: 9533

To load from the assets catalog of the current bundle, use something like:

let image = Bundle(for: type(of: self)).image(forResource: NSImage.Name(“logo"))!

Upvotes: -1

contactritsard
contactritsard

Reputation: 84

Make sure you have the project set to use xcassets instead of just individual resource files. Once you create an image in the xcasset, you should just specify the name of the image, not the path. For example, [NSImage imageNamed:@"StatusBarIcon"]

Upvotes: 3

Related Questions