NSImage - can't load image from file

jrck picture jrck · Mar 24, 2014 · Viewed 11.6k times · Source

I program command-line application that will load image from Supporting Files. Image is copied in Supporting Files, but when I use following code, variable imgC returns nil.

NSString* pathC = @"galaxy.jpg";
NSImage* imgC = [NSImage imageNamed: pathC];

ImgC returns nil even if I use the following code:

NSString* pathC = @"galaxy.jpg";
NSImage* imgC = [[NSImage alloc] initWithContentsOfFile: pathC];

Can someone please help me?

(PS: Sorry for my bad English.)

Many thanks, Peter

Answer

Peter Kreinz picture Peter Kreinz · Sep 13, 2015

Swift Style:

if let imageRef = NSImage(byReferencingFile: "/path/to/galaxy.jpg")
{
    println("image size \(imageRef.size.width):\(imageRef.size.height)")
}