Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 24, 2008, 08:57 AM   #1
DeDMakar
macrumors member
 
Join Date: Mar 2008
Send a message via ICQ to DeDMakar Send a message via Skype™ to DeDMakar
NSImageView setImage

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)MySetImageid)sender;
@end

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

Interface Builder:
IconUser outlets for NSImageView.
MySetImage action for NSButton.
DeDMakar is offline   0 Reply With Quote
Old Mar 24, 2008, 09:19 AM   #2
DeDMakar
Thread Starter
macrumors member
 
Join Date: Mar 2008
Send a message via ICQ to DeDMakar Send a message via Skype™ to DeDMakar
Link must be "/Users/admin/Pictures/apple_big.jpg" . Without ~ .
DeDMakar is offline   0 Reply With Quote
Old Mar 24, 2008, 09:23 AM   #3
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
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
kainjow is offline   0 Reply With Quote
Old Mar 24, 2008, 09:35 AM   #4
CaptainZap
macrumors regular
 
Join Date: Jan 2007
You can convert a ~/ path to a full path by using this method
[@"~/" stringByExpandingTildeInPath];
__________________
CaptainZap is offline   0 Reply With Quote
Old Mar 25, 2008, 12:57 PM   #5
DeDMakar
Thread Starter
macrumors member
 
Join Date: Mar 2008
Send a message via ICQ to DeDMakar Send a message via Skype™ to DeDMakar
If my image is situated in the project folder, the full path may not be specified? Give example, please.
DeDMakar is offline   0 Reply With Quote
Old Mar 25, 2008, 01:28 PM   #6
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
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).
kainjow is offline   0 Reply With Quote
Old Mar 25, 2008, 01:58 PM   #7
Sayer
macrumors 6502a
 
Sayer's Avatar
 
Join Date: Jan 2002
Location: Austin, TX
Add the image to the project, in "Resources", and load the image using:

Code:
NSImage *theImage = [NSImage imageNamed:@"apple_big.jpg"];
__________________
Obama is a true statesman whose experience as a state senator, half-term US Senator & guest lecturer in a Constitutional Law class has fully prepared him to take control of our nuclear arsenal.-Me
Sayer is offline   0 Reply With Quote
Old Mar 26, 2008, 09:32 AM   #8
DeDMakar
Thread Starter
macrumors member
 
Join Date: Mar 2008
Send a message via ICQ to DeDMakar Send a message via Skype™ to DeDMakar
Quote:
Originally Posted by Sayer View Post
Add the image to the project, in "Resources", and load the image using:

Code:
NSImage *theImage = [NSImage imageNamed:@"apple_big.jpg"];
Thank you.
DeDMakar is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Rotate NSImageView around it's center Ides Mac Programming 1 Nov 15, 2013 12:56 AM
NSImageView resize rocotilos Mac Programming 2 Oct 16, 2012 07:34 AM

Forum Jump

All times are GMT -5. The time now is 03:00 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC