PDA

View Full Version : Adding and Removing Images in Obj. C




JOD8FY
Mar 2, 2007, 05:20 PM
Hello all,

As some of you may know, I am new to Objective C, having only programmed with Java in the past. I have made a simple application in Xcode and am really enjoying the learning experience. One thing that I want to do, though, is add an image, pause, and then remove the image when a certain condition is met. I also want to specify where the image appears on the screen. In plain English, I'm looking for something like this:

if(button is pressed)
{
add the image at bounds (200,150);
pause for half a second; // I want the image to remain on the screen for half a second
remove the image;
}

I essentially just want the image to flash over a certain part of the screen to highlight that area. Can anyone help me with this?

Thanks a lot,
JOD8FY



Catfish_Man
Mar 2, 2007, 05:54 PM
If you're trying to draw outside of your window, you'll need to have the button create a title-bar-less window and draw into that.

JOD8FY
Mar 2, 2007, 06:13 PM
Nah, I just want to have the image flash inside the window. Any ideas?

Thanks,
JOD8FY

mduser63
Mar 2, 2007, 06:15 PM
Nah, I just want to have the image flash inside the window. Any ideas?

Thanks,
JOD8FY

I'm honestly not trying to give you a RTFM response, but this really is covered really well in Cocoa Programming for Mac OS X by Aaron Hillegass, which I highly recommend for anyone learning Cocoa.

JOD8FY
Mar 2, 2007, 07:06 PM
Is it a difficult process? I thought it would have been fairly easy, but maybe I'm mistaken. In Java it's just a few simple commands...

I do have one other question. If I want an event to occur depending on the text in a textfield, how would I do this? Essentially if the word in the textfield is "Something", then do event A; if the word in the textfield is "SomethingElse", then do event B. Is there a command for this?

Thanks for all the help so far. Any input on either of the two questions (this one or the adding/removing an image question) would be much appreciated.

JOD8FY

robbieduncan
Mar 3, 2007, 03:47 AM
Is it a difficult process? I thought it would have been fairly easy, but maybe I'm mistaken. In Java it's just a few simple commands...

I do have one other question. If I want an event to occur depending on the text in a textfield, how would I do this? Essentially if the word in the textfield is "Something", then do event A; if the word in the textfield is "SomethingElse", then do event B. Is there a command for this?

Thanks for all the help so far. Any input on either of the two questions (this one or the adding/removing an image question) would be much appreciated.

JOD8FY

There are no built in functions for such exceptionally unique and precision targeted requirements. You'll have to write your own (which is normal). Again I'd recommend reading the documentation, running through and completely understanding the examples and then building from the ground up. If you have never programmed before take a step back and learn the basics: what is a variable, what is a statement, what is an if statement, what are loops, what is a function, what is a class, what is an object... ?

Anyway if we assume you have an instance of a class that has an outlet connect you your textfield that I will assume is like this:


IBOutlet NSTextField *textField;


then we can write simple action method that we will target from our button (or wherever really):


-(IBAction) myAction:(id) sender
{
if ([[textField stringValue] isEqualToString:@"Something"])
{
// Do whatever when it's something. For example (as below) call another action method..
[self mySomethingAction:sender];
}
else
{
// Do other thing when it's not
}
}

robbieduncan
Mar 3, 2007, 03:59 AM
Note to Mods: sorry about replying in two separate replies one after another: they are both quite long and not really related!

Regarding your other question you cannot simply add an image. Your Window hosts NSView subclasses (which until 10.5 is here should not overlap but we'll ignore that for now). An NSImage is not an NSView. An NSImageView however is. So what we now thing we want to do is add an NSImageView which is set to display the image you want at the location you want. That all seems simple and clear (I'll come back to this).

Moving on we want to "pause" and then remove it. Well we certainly don't want to enter a look or sleep our thread for 1 second. All that will do is lock up the UI and make our app seem really poor. What we really want to do is set a timer to fire in one second and call a method to remove the image view. Again that's pretty simple.

So lets look at some code. I'm going to assume that you've managed to get your image somehow and it's available. If it's not well RTFM for that I'm afraid.


// Instance variables
NSImage *myImage; // You will ensure this has your image in it
IBOutlet NSWindow *myWindow; // Connect this to the window

-(IBAction) buttonClicked:(id) sender
{
NSImageView *view = [[[NSImageView alloc] initWithFrame:NSMakeRect(200,150,200,300)] autorelease]; // This assumes you want a 200x300 view....
[view setImage:myImage];
[[myWindow contentView] addSubview:view];
[[myWindow contentView] setNeedsDisplay:YES];
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(removeImage:) userInfo:view repeats:NO];
}

- (void) removeImage:(id) imageView
{
[[myWindow contentView] removeSubview:(NSView *) imageView];
}


Note this and the code in my previous post was typed straight into the reply box. I've not compiled this. It probably has typos!

JOD8FY
Mar 3, 2007, 11:20 AM
Thanks so much robbieduncan! I'll have to give that a try.

JOD8FY

JOD8FY
Mar 3, 2007, 12:59 PM
Okay, a few errors. I was able to implement the first question (the one where I wanted something to occur based on what was in the textfield) and all went well. But as for adding and removing the image, Xcode is giving me some errors. What I actually want to do is combine my two questions so that depending on what word is in the textfield, the image will appear and disappear in a certain area in the window. Heres my code:

(The name of the image is "spotlight" and the window's name is Window).


NSImage *spotlight;
IBOutlet NSWindow *Window;
- (IBAction)find:(id)sender
{
if([[searchBox stringValue] isEqualToString:@"TestA"])
{
NSImageView *view = [[[NSImageView alloc] initWithFrame:NSMakeRect(200,150,200,300)] autorelease];
[view setImage:spotlight];
[[Window contentView] addSubview:view];
[[Window contentView] setNeedsDisplay:YES];
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(removeImage:) userInfo:view repeats:NO];
}
- (void)removeImage:(id)imageView
{
[[Window contentView] removeSubview: (NSView *) imageView];
}
}


I've tried putting removeImage both inside and outside of (IBAction)find:. If it's inside, I get the error that removeImage is undeclared (first use in this function). If it's outside, I get a warning that says no "-removeSubview" method found. I'm also getting a warning that there is an unused variable "timer".

Any suggestions?

Thanks a lot,
JOD8FY

robbieduncan
Mar 3, 2007, 01:22 PM
removeImage is a separate method. It must be outside of the other method declaration but inside the class definition. If you don't know that I'd suggest starting all over again with basic Objective-C.

As I said it was typed straight into the browser. The correct implementation is clear if you read the documentation:


- (void)removeImage:(id)imageView
{
[imageView removeFromSuperview];
}


Edit to add: you don't actually need "NSTimer *timer =". We're not using the variable anywhere so it's unused and unnecessary.