views:

201

answers:

4

I am having a problem that is eating me alive. I really hope I am just missing something small here. It appears to be a rather "n00b" issue.

I have a blank NSImageView that I want to display a picture when a button is pressed — simple as that.

Here is my line of coding

NSBundle *mb = [NSBundle mainBundle];
NSString *fp = [mb pathForResource:@"tiles" ofType:@"PNG"];
NSImage *image = [[NSImage alloc] initWithContentsOfFile:fp];
if ( [image isValid] ) {
    [selection setImage:image];
    [selection setImageScaling:NSScaleProportionally];
}

Whereas,

tiles.PNG is a resource in my bundle

and if [image isValid] is satisfied, because I've inserted dummy code into the clause and had that work

selection is defined in my header file as follows

IBOutlet NSImageView *selection;

It is also linked up to the application delegate in IB.

I have a feeling I might not be linking it properly?

WHy wouldn't the image display? If anyone can see an error - or provide me with working code - I would be soooooo thankful

Brian

A: 

did you remember to send -setNeedsDisplay to the NSImageView after you set the image?

NSResponder
No, is that necessary? I added in [selection setNeedsDisplay:YES] and still got nothing
Brian
No, it is not necessary.
benzado
+1  A: 

It's not a linking issue—your app wouldn't even launch (assuming it even links successfully) if you'd failed to link against Cocoa or AppKit.

More probably, either you haven't connected the outlet to your image view in your nib, or you haven't loaded the nib yet. The way to check this would be to NSLog the value of the imageView pointer, using the %p formatter.

Peter Hosey
Should there be more than one link in IB? I went to the appDelegate icon in IB, saw the "selection" outlet I defined in my .h file, and dragged it to the NSImage. Now it shows linked. Does there need to be anything more linked up here? I know the action is linked up and functioning because it does other things besides change an image... all of which work.I wish I could take a screen shot, but my apple "preview" application is corrupt.
Brian
NSImage is not NSImageView. And if your Preview application is corrupt, it's probably not the only part of your Mac OS X installation that's corrupt. I conclude that there is a reinstall in your future.
Peter Hosey
+1  A: 

I had a similar issue where my view wasn't displaying, and it turned out that the view was hidden. This was a setting in the view properties in Interface Builder. Just a punt, but give it a go.

Tony van der Peet
+1  A: 

You need to use the debugger and see what's going on as it runs. Is fp nil? Is image nil? Is selection nil? The debugger is your friend.

Ken Aspeslagh