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 > Displaying an NSImage to the screen

Displaying an NSImage to the screen
Thread Tools
Professional Poster
Join Date: Apr 2001
Location: Long Beach, CA
Status: Offline
Reply With Quote
Apr 3, 2002, 06:41 PM
 
Okay, I give up. I'm resorting to asking the board... again...

I'm opening images from the hard drive using -initByReferenceToFile: and then I'm trying to display the image on the screen. I've tried many many many different methods, and I give up. What am I supposed to do? I tried just giving the image to an NSImageView, but it didn't show up... I tred using an NSView and locking focus, telling it to draw in a rectangle, and unlocking focus, etc... I've even tried using an NSImageRep in addition to the NSImage, because I thought I saw somewhere that it was needed.

How is it supposed to be done?

ACSA 10.4/10.3, ACTC 10.3, ACHDS 10.3
     
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Apr 3, 2002, 11:01 PM
 
Easiest way is to subclass NSView, override it's drawRect method, and then call compositeToPoint (or something like that) off of your NSImage instance.

HTH,
F-bacher
     
Detrius  (op)
Professional Poster
Join Date: Apr 2001
Location: Long Beach, CA
Status: Offline
Reply With Quote
Apr 3, 2002, 11:17 PM
 
There's nothing prepackaged in Cocoa so that I can simply put an image up? NSImageView inside of an NSScrollView won't do it? When I do it that way, I can drop images on it, but I can't manually open images and have them display from within the code. I'm wanting the user to be able to open multiple images without creating a new window for each one and then individually dropping each image on the window.

(I will actually need to follow your recommendation at a different point in my code though, thank you.)

ACSA 10.4/10.3, ACTC 10.3, ACHDS 10.3
     
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Apr 4, 2002, 07:33 AM
 
[imageView setImage:yourImageInstance] doesn't work?
You might want to send it a setNeedsDisplay:YES message to make sure it updates (but I don't think it's necessary).

More likely the porblem is with your instance of NSImage. Try this code:

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
if(yourImageInstance==nil) NSLog(<font color = orange>@"Uh oh, NSImage instance is nil!"</font>);
</font>[/code]

I'm betting that message will print out.

HTH,
F-bacher
     
Detrius  (op)
Professional Poster
Join Date: Apr 2001
Location: Long Beach, CA
Status: Offline
Reply With Quote
Apr 4, 2002, 07:56 AM
 
I've got checks all over the place checking from problems like that. According to all the messages I have printing out, the NSImage is valid. In addition, the checks I have on the actual drawing are claiming that the draw was actually successful, but it doesn't show up.

I've been using the methods you recommended, and I inserted an additional check right before assigning the NSImage to the NSImageView and it still claims that it's okay.


But it doesn't show up in the NSImageView.

ACSA 10.4/10.3, ACTC 10.3, ACHDS 10.3
     
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Apr 4, 2002, 12:47 PM
 
Hehe, I've got another idea. Many times I don't hook up my IB outlets with my code properly (name spelled wrong).

Check to see if your instance of NSImageView is equalt to nil. if that's the case, then the problem is with IB, probably just a naming problem.

HTH,
F-bacher[/LIST]
     
Detrius  (op)
Professional Poster
Join Date: Apr 2001
Location: Long Beach, CA
Status: Offline
Reply With Quote
Apr 5, 2002, 02:57 PM
 
Originally posted by Ghoser777:
<STRONG>Hehe, I've got another idea. Many times I don't hook up my IB outlets with my code properly (name spelled wrong).

Check to see if your instance of NSImageView is equalt to nil. if that's the case, then the problem is with IB, probably just a naming problem.

HTH,
F-bacher[/LIST]</STRONG>
How would naming cause problems?

Anyway, I've decided to take a different approach to this. I have another area in the program where I am going to have to draw images differently anyway, since I want multiple images in the same view. If I were to subclass NSImageView and treat it as an NSView -- that is, create the draw function myself -- would I still get the functionality that a user would be able to simply drop an image in the view and I would get a reference to it?

ACSA 10.4/10.3, ACTC 10.3, ACHDS 10.3
     
Detrius  (op)
Professional Poster
Join Date: Apr 2001
Location: Long Beach, CA
Status: Offline
Reply With Quote
Apr 9, 2002, 11:36 PM
 
Originally posted by Ghoser777:
<STRONG>Hehe, I've got another idea. Many times I don't hook up my IB outlets with my code properly (name spelled wrong).

Check to see if your instance of NSImageView is equalt to nil. if that's the case, then the problem is with IB, probably just a naming problem.

HTH,
F-bacher[/LIST]</STRONG>

Okay, so I've finally had a chance to re-implement this stuff. I've now subclasses NSView, written the code, imported the header into Interface Builder, created an NSView in my window, told it that it's supposed to be my subclass... and since I was getting segmentation faults, I traced it back the NSView subclass... and the variable is nil. Oh, and I did hook up the outlet.

Maybe I need a paradigm shift or something. I just can't seem to get this to work.

what is the proper method for instantiating an NSView subclass? Assume the code is written (and properly). How am I supposed to put it together to make it work? (BTW, if the code is faulty, which it probably is, I'll find it myself eventually. I'm not accustomed to debugging user interfaces. I can debug code, though.)

ACSA 10.4/10.3, ACTC 10.3, ACHDS 10.3
     
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Apr 10, 2002, 12:02 AM
 
Hmmm... something's fishy. Could I possibly take a look at your basic code? It's the easiest way for me to get a feel for what's going wrong. You can email it to me at fahrenba@uiuc.edu

F-bacher
     
Detrius  (op)
Professional Poster
Join Date: Apr 2001
Location: Long Beach, CA
Status: Offline
Reply With Quote
Apr 10, 2002, 11:50 PM
 
For those curious about the solution:

any of an NSWindowController's init functions are called BEFORE the Nib file has been loaded. Therefore, none of the outlets are valid pointers yet. While an NSDocument has a function called -windowControllerDidLoadNib, NSWindowControllers don't have the exact same function. -windowDidLoad. Might as well be the same thing. It's called after the Nib file is loaded. Therefore, if you want to set an NSImage to be part of an NSView, you do it here. Otherwise, the view doesn't exist yet, so you can't put the image in the view.


duh. why didn't I think of that in the first place??

ACSA 10.4/10.3, ACTC 10.3, ACHDS 10.3
     
Fresh-Faced Recruit
Join Date: Mar 2002
Status: Offline
Reply With Quote
Apr 11, 2002, 02:09 PM
 
As a Postscript to all this....

I had a similar problem with a simple "forms" based application where the outlets were not connected in the -(id)init member.

To fix this just implement an '-(void)awakeFromNib' member (part of the NSNibAwaking protocol) your class and this will be called automatically with all outlets connected.

Cocoa is cool!
     
   
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 12:32 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