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 images in obj C

Displaying images in obj C
Thread Tools
Forum Regular
Join Date: Jan 2001
Location: San Luis Obispo, California, USA
Status: Offline
Reply With Quote
Apr 30, 2001, 01:08 PM
 
Ok, I can display images into a custom NSImageView using Java.

I can even switch images using a button.

But I can't do it in Objective C. I tried translating my image-switching code from java to obj-c, but that was too big.... so I decided to start smaller, and see if I can simply display images in obj-c (before I try to switch images or animate them)

Well, It compiles, but it doesn't do anything. It doesn't display my image. You can check out my code here. (it's a .dmg, so if you're using IE you have to do a save link as.)

Can anyone tell me what I'm doing wrong? Where can I find some examples that display images in objective C? Where can I find some documentation on using NSImageView?

Thanks,
Nathaniel
     
Admin Emeritus
Join Date: Oct 2000
Location: Boston, MA
Status: Offline
Reply With Quote
Apr 30, 2001, 02:30 PM
 
Ah, a few problems.

First of all, as a general rule, capitalize the first letter of each class you define. Instances of those classes should be non-capitalized. (e.g. a Box named box.) Obviously, this won't fix any errors, but it makes your code cleaner and easy to understand (i.e. I see something captialized, it's a class...)

Second, about the way things are hooked up. Your controller shouldn't be hooked up to something called "displayImage." Hooking up something to an outlet only means that you have an instance variable which is initialized properly and that you send messages to.

Third, there's no need to subclass the image view. You just want to send it a message telling it to display a certain image. NSImageView already has that method built-in.

Fourth, if you want something to receive a notification, you should hook it up to the File's Owner as its delegate.

Let's start from the beginning, here's how it _should_ look:

You have a fresh project. Open up the nib file, and subclass NSObject, name it "Controller." Add an outlet called "imageView." Create the files necessary... Instantiate the Controller. Drag an NSImageView from the Cocoa palette onto your window, but don't subclass it.

Right now, you have all the objects you need.
The File's Owner has an outlet called "delegate." Connect that up to your controller. It'll now receive the notification.
The Controller should have an outlet called "imageView." Connect that up to your NSImageView. That will let it communicate to the imageView.
Now, in your Controller file, create the applicationDidFinishLaunching: just like you did, with the following code:

Code:
- (void)applicationDidFinishLaunching :(NSNotification *)not { [imageView setImage:[NSImage imageNamed:@"ImageName"]]; }
Try that. Although getting the image's path from the bundle will work, imageNamed is a much quicker solution.


- EDIT: Darn smilies! :-)

------------------


http://gilgalad.dyndns.org/

[This message has been edited by parallax (edited 04-30-2001).]
"Against stupidity, the gods themselves contend in vain" (Schiller)
     
robotic  (op)
Forum Regular
Join Date: Jan 2001
Location: San Luis Obispo, California, USA
Status: Offline
Reply With Quote
Apr 30, 2001, 04:50 PM
 
Thanks! That works great, and it's much simpler than I was trying to make it.

Thanks for you help!

-Nathaniel
     
Fresh-Faced Recruit
Join Date: Feb 2001
Location: Amstelveen, Netherlands
Status: Offline
Reply With Quote
May 3, 2001, 02:47 AM
 
Hello,

I also tried to follow the example. But when I run the program it doesn´t show the image.

What exactly do I have to do to bring in the picture into the project?

Thanks
     
robotic  (op)
Forum Regular
Join Date: Jan 2001
Location: San Luis Obispo, California, USA
Status: Offline
Reply With Quote
May 3, 2001, 03:03 AM
 
As I mentioned in my email to you, you have to add the pictures to the project (project menu: add files).... I'm repeating it here in case anyone else is having the same problem.

If anyone still can't get it to work, I have a working example I can email.

(I want to eventually get the basics down for a 2d game... release it as open source, then see what people do with it. Right now, I can display images and move them. Now I just have to learn how to use timers, and how to display several images at the same time, and how to control them with keyboard and mouse, and you have the basics of a game)


-robotic
     
   
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:40 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