NSImageView color

Discussion in 'Mac Programming' started by wotmania505, Feb 28, 2011.

  1. wotmania505 macrumors newbie

    Joined:
    Feb 28, 2011
    Location:
    Michigan
    #1
    So, I've recently started working in Xcode. I wanted to make a window with an image loaded in the background, with another smaller image on top of it, so I could move the top image around and see the rest of the bottom image.

    My process was to use the setImage method of NSImageView to load each image into an NSImageView in my interface. Each image is of type .png, and is referenced through an NSImage object created using 'imageNamed:mad:"string here"'.


    Here's the problem: One image (the bottom one) loads fine, but the top image comes out in greyscale, rather than in color. This is odd because the images both have color, and both show up correctly in the xcode project window.

    I can't figure out why this is happening - Does anyone have any suggestions, or even a guess? I get the feeling that I must be doing something wrong, or have accidentally messed something up, but I can't figure out what...
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    Only thing I can think of is to add the image to a new blank project, and add it to an NSImageView through the nib and see what that shows.
     
  3. wotmania505 thread starter macrumors newbie

    Joined:
    Feb 28, 2011
    Location:
    Michigan
    #3
    Testing

    So, I created a new project, and added the image file to it. The project has:

    classes:
    Test1AppDelegate.h
    Test1AppDelegate.m

    resources:
    MainMenu.xib
    theGuyTemplate.png

    plus whatever else xcode created. The only changes I made were


    1: opened MainMenu.xib and added an NSImageView to the window by draggin it from the library,.

    2: Added an instance variable theImageView to Test1AppDelegate.h, and added

    Code:
    @property (assign) IBOutlet NSImageView *theImageView;
    3: added
    Code:
    @synthesize theImageView;
    to Test1AppDelegate.m, and implemented

    Code:
    -(void)awakeFromNib
    {
    NSImage* theImage = [NSImage imageWithName:@"TheGuyTemplate.png"];
    [theImageView setImage:theImage];
    }
    
    4: Loaded the class File into Interface Builder, and connected Test1 App Delegate to the NSImageView created earlier with the outlet theImageView.

    It builds and runs, but again I have the problem of the image appearing in greyscale. Any other Ideas? Attached is the image I'm working with - maybe there is something wrong with it...
     

    Attached Files:

  4. McGordon macrumors member

    Joined:
    Dec 28, 2010
    Location:
    Scotland
    #4
    I had to change the method name from imageWithName to imageNamed to get it to work. Is imageWithName an old deprecated method? I'm on 10.6.6.

    Code:
    [NSImage imageWithName:@"TheGuyTemplate.png"];
    // Change to:
    [NSImage imageNamed:@"TheGuyTemplate.png"];
    
    I get the same as you, a greyscale image. Other images work fine in colour. If I make it editable and drag your image to the image well, it works in colour too.

    I re-saved it in photoshop elements, save for web: png-8, 16 colours adaptive, transparency and it worked. I don't know how you saved the image originally, but it opens fine in everything except in that NSImageView.

    I've attached my re-saved image for you.
     

    Attached Files:

  5. wotmania505 thread starter macrumors newbie

    Joined:
    Feb 28, 2011
    Location:
    Michigan
    #5
    Solved!

    Thanks! Your altered image worked fine. You're also right - imageWithName is deprecated.. I don't know why I used it here, since I used imageNamed in my code. I created my image using GIMP, and saving as .png. Now I'm curious as to what went wrong... Thanks again!
     

Share This Page