NSImageView setImage

Discussion in 'Mac Programming' started by DeDMakar, Mar 24, 2008.

  1. macrumors member

    Joined:
    Mar 24, 2008
    #1
    I'm sorry if a basic question.
    How to insert in NSImageView image? This code does not work:
    Xcode:
    @interface MyInitMainWindow : NSObject {
    NSImage *StrIconUser;
    IBOutlet NSImageView *IconUser;
    }
    -(IBAction)MySetImage:(id)sender;
    @end

    @implementation MyInitMainWindow
    -(IBAction)MySetImage:(id)sender
    {
    [StrIconUser initWithContentsOfFile:mad:"~/Users/admin/Pictures/apple_big.jpg"];
    [IconUser setImage:StrIconUser];
    }
    @end

    Interface Builder:
    IconUser outlets for NSImageView.
    MySetImage action for NSButton.
     
  2. thread starter macrumors member

    Joined:
    Mar 24, 2008
    #2
    Link must be "/Users/admin/Pictures/apple_big.jpg" . Without ~ . :confused:
     
  3. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #3
    The ~ character represents the user's home folder, which would be /Users/admin in your case. So the path would actually be ~/Pictures/apple_big.jpg, but in Cocoa ~ doesn't get translated, so you need to use the full path of /Users/admin/Pictures/apple_big.jpg
     
  4. macrumors regular

    Joined:
    Jan 17, 2007
    #4
    You can convert a ~/ path to a full path by using this method
    [@"~/" stringByExpandingTildeInPath];
     
  5. thread starter macrumors member

    Joined:
    Mar 24, 2008
    #5
    If my image is situated in the project folder, the full path may not be specified? Give example, please.
     
  6. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #6
    The full path must always be specified. You just don't always have to hard code it into your app.

    Look into the NSBundle, NSImage documentation. They provide everything you need to load images without needing to know the absolute path (if your image is inside your .app's Resources folder).
     
  7. macrumors 6502a

    Sayer

    Joined:
    Jan 4, 2002
    Location:
    Austin, TX
    #7
    Add the image to the project, in "Resources", and load the image using:

    Code:
    NSImage *theImage = [NSImage imageNamed:@"apple_big.jpg"];
     
  8. thread starter macrumors member

    Joined:
    Mar 24, 2008
    #8
    Thank you.
     

Share This Page