Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Working with NSImageView

Working with NSImageView
Thread Tools
Posting Junkie
Join Date: May 2001
Location: Portland, OR
Status: Offline
Reply With Quote
Jan 16, 2002, 10:12 AM
 
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?
     
goMac  (op)
Posting Junkie
Join Date: May 2001
Location: Portland, OR
Status: Offline
Reply With Quote
Jan 16, 2002, 05:25 PM
 
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
Reply With Quote
Jan 18, 2002, 04:18 PM
 
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.
     
goMac  (op)
Posting Junkie
Join Date: May 2001
Location: Portland, OR
Status: Offline
Reply With Quote
Jan 20, 2002, 02:18 PM
 
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
Reply With Quote
Jan 20, 2002, 03:22 PM
 
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] initWithContentsOfFileath] (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] initWithContentsOfFileath];
}

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.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:44 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2