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

DeDMakar

macrumors member
Original poster
Mar 24, 2008
39
0
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.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
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
 

CaptainZap

macrumors regular
Jan 17, 2007
170
0
You can convert a ~/ path to a full path by using this method
[@"~/" stringByExpandingTildeInPath];
 

DeDMakar

macrumors member
Original poster
Mar 24, 2008
39
0
If my image is situated in the project folder, the full path may not be specified? Give example, please.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
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).
 

Sayer

macrumors 6502a
Jan 4, 2002
981
0
Austin, TX
Add the image to the project, in "Resources", and load the image using:

Code:
NSImage *theImage = [NSImage imageNamed:@"apple_big.jpg"];
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.