Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 2, 2007, 05:20 PM   #1
JOD8FY
macrumors 6502a
 
JOD8FY's Avatar
 
Join Date: Mar 2004
Location: United States
Adding and Removing Images in Obj. C

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
__________________
1 Ghz PowerBook G4 12" : 768 MB RAM : 40 GB HD : 32 MB VRAM
20" ACD : Apple Wireless Keyboard & Mouse : Final Cut Studio
Tiger 10.4.10
JOD8FY is offline   0 Reply With Quote
Old Mar 2, 2007, 05:54 PM   #2
Catfish_Man
macrumors 68030
 
Catfish_Man's Avatar
 
Join Date: Sep 2001
Location: Portland, OR
Send a message via AIM to Catfish_Man
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.
Catfish_Man is offline   0 Reply With Quote
Old Mar 2, 2007, 06:13 PM   #3
JOD8FY
Thread Starter
macrumors 6502a
 
JOD8FY's Avatar
 
Join Date: Mar 2004
Location: United States
Nah, I just want to have the image flash inside the window. Any ideas?

Thanks,
JOD8FY
__________________
1 Ghz PowerBook G4 12" : 768 MB RAM : 40 GB HD : 32 MB VRAM
20" ACD : Apple Wireless Keyboard & Mouse : Final Cut Studio
Tiger 10.4.10
JOD8FY is offline   0 Reply With Quote
Old Mar 2, 2007, 06:15 PM   #4
mduser63
macrumors 68040
 
mduser63's Avatar
 
Join Date: Nov 2004
Location: Salt Lake City, UT
Send a message via AIM to mduser63
Quote:
Originally Posted by JOD8FY View Post
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.
mduser63 is offline   0 Reply With Quote
Old Mar 2, 2007, 07:06 PM   #5
JOD8FY
Thread Starter
macrumors 6502a
 
JOD8FY's Avatar
 
Join Date: Mar 2004
Location: United States
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
__________________
1 Ghz PowerBook G4 12" : 768 MB RAM : 40 GB HD : 32 MB VRAM
20" ACD : Apple Wireless Keyboard & Mouse : Final Cut Studio
Tiger 10.4.10
JOD8FY is offline   0 Reply With Quote
Old Mar 3, 2007, 03:47 AM   #6
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Quote:
Originally Posted by JOD8FY View Post
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:

Code:
IBOutlet NSTextField *textField;
then we can write simple action method that we will target from our button (or wherever really):

Code:
-(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 is offline   0 Reply With Quote
Old Mar 3, 2007, 03:59 AM   #7
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
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.

Code:
// 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!
robbieduncan is offline   0 Reply With Quote
Old Mar 3, 2007, 11:20 AM   #8
JOD8FY
Thread Starter
macrumors 6502a
 
JOD8FY's Avatar
 
Join Date: Mar 2004
Location: United States
Thanks so much robbieduncan! I'll have to give that a try.

JOD8FY
__________________
1 Ghz PowerBook G4 12" : 768 MB RAM : 40 GB HD : 32 MB VRAM
20" ACD : Apple Wireless Keyboard & Mouse : Final Cut Studio
Tiger 10.4.10
JOD8FY is offline   0 Reply With Quote
Old Mar 3, 2007, 12:59 PM   #9
JOD8FY
Thread Starter
macrumors 6502a
 
JOD8FY's Avatar
 
Join Date: Mar 2004
Location: United States
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).

Code:
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
__________________
1 Ghz PowerBook G4 12" : 768 MB RAM : 40 GB HD : 32 MB VRAM
20" ACD : Apple Wireless Keyboard & Mouse : Final Cut Studio
Tiger 10.4.10
JOD8FY is offline   0 Reply With Quote
Old Mar 3, 2007, 01:22 PM   #10
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
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:

Code:
- (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.
robbieduncan is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
UIScrollView+images removing problem erdinc27 iPhone/iPad Programming 0 Nov 29, 2013 07:12 AM
Adding Images to Text Edit Huntn Mac Applications and Mac App Store 5 Mar 20, 2013 11:05 AM
Adding Images To Contacts In Sparrow DarkVictory iPhone and iPod touch Apps 1 Nov 28, 2012 01:28 AM
Prompted to enter password when adding/removing files from Mac HD Shotgun OS Mac Basics and Help 1 Aug 7, 2012 02:27 AM

Forum Jump

All times are GMT -5. The time now is 03:20 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC