Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
After many attempts and not getting anything displayed in my NSImageView I found this link http://stackoverflow.com/questions/5237703/displaying-an-image-in-cocoa and it was just about what I had but I still get nothing. The result of this is crashing the program with this error "unrecognized selector sent to instance" I know enough from you guys that it is sending to an object that is not there. But if I instantiate it 'myBackGroundView = [[NSImageView alloc] init];' no crash but no image? In IB I dragged out a Custom View from the library and did the connections.

header file
Code:
 IBOutlet NSImageView *myBackGroundView;

Code:
-(void)awakeFromNib{ 
    NSString *imagePath = @"/Users/larspro/Documents/xpImages/xpbackground.png";
    
    if (imagePath == nil) { //check a valid path.
        NSLog(@"NO IMAGE");
    }
    
    NSImage *testImage = [[[NSImage alloc] initWithContentsOfFile:imagePath] autorelease];
    
    if ( [testImage isValid] ) {
        [myBackGroundView setImage:testImage];
           }
    else
    {
        NSLog(@"Not Working up");
    }
 
"unrecognized selector sent to instance" means that a method is being called on an object that doesn't actually have a method with that signature defined on it. This could be a result of several things, but the first thing I would verify is that everything is wired up correctly in Interface Builder.
 
FInd out if the image view is actually retaining the image. The documentation does not say one way or the other. Remove the autorelease and see if the error goes away. Then put a release after the setImage: and see if the image is actually released.
 
Here is a photo of my connection. I declared my Instance variable in the header file of the TableVewController which most of the code is written in. Then to get and set the image, the code is performed in the Implementation file in the awakeFromNib Method. TableViewController is a subclass of Entry and Entry is a subclass of NSObject
 

Attachments

  • myController.jpg
    myController.jpg
    96 KB · Views: 192
I removed the AutoRelease and created my own [testImage Release] and it still crashes.
 
I created a new simple project and I zipped it down and included it. It is still causing the problem. If you open it you will need to link to a different image to test it?

Before I run the program I see an error warning that says it may not respond to setImage. I did misspell the instance variable name but it is the same spelling for all.

This is so simple and I can't understand why it won't work
 

Attachments

  • testImage.zip
    37 KB · Views: 120
I just looked at the NSImage Class Reference and I don't see a setImage method anywhere in the documentation. I do however see the setImage method in the NSImageView documentation.
 
I think I found what I was doing wrong. I was using an NSCustomView and that was giving me the error with setImage. I then did a search in the library for NSImage and Image Well came up. I deleted my NSCustomView and replaced it with an Image Well, atttached the code and the photo appeared.

Sorry for the confusion folks. I guess I misunderstood what the NSCustomView is for. But the image Well seems to work.

thanks
 
This is so simple and I can't understand why it won't work

Your sample project doesn't work because you don't have an NSImageView anywhere in your nib, nor is your imageView outlet declared as an NSImageView. If I put an actual NSImageView in your nib, change the NSView outlet to an NSImageView, and hook the two together, everything works fine.

EDIT: Guess I should have refreshed the page before I replied..
 
Thanks... I found that out. the good news was that the setImage warning gave me a clue as to what was wrong. I am just now starting to play with images and still learning.
 
Thanks... I found that out. the good news was that the setImage warning gave me a clue as to what was wrong. I am just now starting to play with images and still learning.

Go into "Build Settings" and turn all warnings into errors. I NEVER build anything any other way.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.