PDA

View Full Version : NSImageView setImage




DeDMakar
Mar 24, 2008, 08:57 AM
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:@"~/Users/admin/Pictures/apple_big.jpg"];
[IconUser setImage:StrIconUser];
}
@end

Interface Builder:
IconUser outlets for NSImageView.
MySetImage action for NSButton.



DeDMakar
Mar 24, 2008, 09:19 AM
Link must be "/Users/admin/Pictures/apple_big.jpg" . Without ~ . :confused:

kainjow
Mar 24, 2008, 09:23 AM
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
Mar 24, 2008, 09:35 AM
You can convert a ~/ path to a full path by using this method
[@"~/" stringByExpandingTildeInPath];

DeDMakar
Mar 25, 2008, 12:57 PM
If my image is situated in the project folder, the full path may not be specified? Give example, please.

kainjow
Mar 25, 2008, 01:28 PM
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
Mar 25, 2008, 01:58 PM
Add the image to the project, in "Resources", and load the image using:

NSImage *theImage = [NSImage imageNamed:@"apple_big.jpg"];

DeDMakar
Mar 26, 2008, 09:32 AM
Add the image to the project, in "Resources", and load the image using:

NSImage *theImage = [NSImage imageNamed:@"apple_big.jpg"];

Thank you.