 |
 |
Working with NSImageView
|
 |
|
 |
|
Posting Junkie
Join Date: May 2001
Location: Portland, OR
Status:
Offline
|
|
Hello. I'm working with NSImageView and I'm running into problems. I declare a NSImage, pull the image from a file in the resources (yes, I leave off the extension), set the NSImageView for the NSImage via a outlet, and que the NSImageView for a refresh. I don't have the code with me, but I'll post it latter today.
The problem is, the image never shows up. If I drag the image on in IB, it works... I'm a cocoa newbie, so anything I'm missing thats a common mistake? As I said, I'll post the code later today...
|
|
8 Core 2.8 ghz Mac Pro/GF8800/2 23" Cinema Displays, 3.06 ghz Macbook Pro
Once you wanted revolution, now you're the institution, how's it feel to be the man?
|
| |
|
|
|
 |
|
 |
|
Posting Junkie
Join Date: May 2001
Location: Portland, OR
Status:
Offline
|
|
Okay.... Heres the code:
#import "thememenucontroller.h"
@implementation thememenucontroller
- (IBAction)itemchosen  id)sender
{
NSImage *temp;
//[picture setImage:nil];
if([[menuinput titleOfSelectedItem] isEqualToString:@"Aqua/Turn Duality Off"])
{
NSLog(@"Aqua Selected");
temp = [NSImage imageNamed:@"Aqua"];
[picture setImage:temp];
[picture setNeedsDisplay:YES];
}
}
@end
The NSLog triggers, so I know the if is working. The one deal is though, that this is a preference pane. This works fine if you compile as a normal application.
Any Ideas?
[ 01-16-2002: Message edited by: goMac ]
|
|
8 Core 2.8 ghz Mac Pro/GF8800/2 23" Cinema Displays, 3.06 ghz Macbook Pro
Once you wanted revolution, now you're the institution, how's it feel to be the man?
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status:
Offline
|
|
If it's a preferences pane, then the main bundle will be the bundle of the System Preferences application, not your pane. So it's looking for Aqua in /Applications/System Preferences.app/Contents/Resources, not in /Library/PreferencePanes/foo/or/whatever
You'll need to use [[NSBundle bundleWithIdentifier:@"com.your.bundleidentifier"] pathForResource:@"Aqua" ofType:"tiff"] and feed that into NSImage's method for initialising with contents of files. Or something similar.
|
|
|
| |
|
|
|
 |
|
 |
|
Posting Junkie
Join Date: May 2001
Location: Portland, OR
Status:
Offline
|
|
Originally posted by Angus_D:
<STRONG>If it's a preferences pane, then the main bundle will be the bundle of the System Preferences application, not your pane. So it's looking for Aqua in /Applications/System Preferences.app/Contents/Resources, not in /Library/PreferencePanes/foo/or/whatever
You'll need to use [[NSBundle bundleWithIdentifier:@"com.your.bundleidentifier"] pathForResource:@"Aqua" ofType:"tiff"] and feed that into NSImage's method for initialising with contents of files. Or something similar.</STRONG>
Thanks. I'm closer now.
But, it still can't find the image. It clears whatever image was there, so I know its trying. Heres the updated code:
#import "thememenucontroller.h"
@implementation thememenucontroller
- (IBAction)itemchosen  id)sender
{
NSImage *temp;
//[picture setImage:nil];
if([[menuinput titleOfSelectedItem] isEqualToString:@"Aqua/Turn Duality Off"])
{
NSLog(@"Aqua Selected");
temp = [NSImage imageNamed:[[NSBundle bundleWithIdentifier:@"com.conundrum.duality4Pref"] pathForResource:@"Aqua" ofType:@"tiff"]];
[picture setImage:temp];
[picture setNeedsDisplay:YES];
}
}
@end
I have set my identifier in build settings to "com.conundrum.duality4Pref" (I typed in the whole thing). Please help! This is my first big Cocoa project.
|
|
8 Core 2.8 ghz Mac Pro/GF8800/2 23" Cinema Displays, 3.06 ghz Macbook Pro
Once you wanted revolution, now you're the institution, how's it feel to be the man?
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status:
Offline
|
|
Um, you don't want to use NSImage's +imageNamed: method when you're a bundle in another application (unless you want to use one of the app's images). That only looks in the application bundle. It will not use the full path for the file that you're trying to give it.
First, make sure that the path returned from -pathForResource:ofType: is in fact the correct path to your image (i.e. NSLog it). If the identifier isn't working, there is always [NSBundle bundleForClass:[self class]]. If the path is correct, then the bundle stuff is fine.
You'll want to use either [[NSImage alloc] initWithContentsOfFile  ath] (or maybe the -initByReferencingFile: variant... probably doesn't matter for your usage). You should also keep your image around in a variable... +imageNamed does this for you but you don't want to use that (as mentioned).
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
- (NSImage *)_aquaImage
{
static NSImage *aquaImage = nil;
if (aquaImage == nil) {
NSBundle *myBundle = [NSBundle bundleWithIdentifier:...];
NSString *path = [myBundle pathForResource:<font color = orange>@"Aqua<font color = red>"</font> ofType:<font color = orange>@"</font>tiff"</font>];
<font color = brown>// store, retained, in global ivar</font>
aquaImage = [[NSImage alloc] initWithContentsOfFile  ath];
}
return aquaImage;
}
- (IBAction)itemchosen(id)sender
{
if([[menuinput titleOfSelectedItem] isEqualToString:<font color = orange>@"Aqua/Turn Duality Off"</font>])
{
NSLog(<font color = orange>@"Aqua Selected"</font>);
[picture setImage:[self _aquaImage]];
[picture setNeedsDisplay:YES];
}
}
</font>[/code]
You probably don't need the setNeedsDisplay: call either, but I'm not sure.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|