NSImageView fails to setImage?

1.8k Views Asked by At

I'm trying to create a simple ImageView that just loads an image from disk, but this isn't working (I put it in my applicationDidFinishLoading):

NSString *file = [@"~/update.png" stringByStandardizingPath];
NSImage *image = [[NSImage alloc] initWithContentsOfFile:file];
[wView setImage:image];

Apparently the image for the NSImageView isn't being set, because if I NSLog [wView image], I get (null). I'm pretty sure I'm making a beginner mistake here, but what?

The file and image load fine; if I draw the image to an NSView it shows up fine. wView isn't nil, and I tried making another project with the default window's view a custom NSImageView subclass whose initWithFrame: calls setImage. Still nothing.

2

There are 2 best solutions below

0
On

Most likely, wView is nil. The most common reason for this is when a class that's instantiated in the nib with a connection to a view also gets instantiated a second time in code.

2
On

(OP here with an actual account)

So it turns out that setImage doesn't work on an NSImageView that's explicitly constructed in IB, if you get what I mean. I made an NSView subclass that constructs an NSImageView inside it and calls setImage, and that worked fine. Really annoying.