NSImage not displaying?

Discussion in 'Mac Programming' started by larswik, Sep 5, 2011.

  1. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    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");
        }
    
    
     
  2. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #2
    "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.
     
  3. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #3
    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.
     
  4. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #4
    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
     

    Attached Files:

  5. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #5
    I removed the AutoRelease and created my own [testImage Release] and it still crashes.
     
  6. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #6
    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
     

    Attached Files:

  7. Shawnpk macrumors 6502

    Joined:
    Jan 13, 2011
    Location:
    Los Angeles, CA
    #7
    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.
     
  8. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #8
    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
     
  9. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #9
    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..
     
  10. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #10
    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.
     
  11. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #11
    Go into "Build Settings" and turn all warnings into errors. I NEVER build anything any other way.
     

Share This Page