Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-16-2010, 06:04 AM   #1 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 15
Amanda521 is on a distinguished road
Default Can't 'talk' to UIImage created in code.

Hi, I'm new to this forum (and the Iphone SDK) and want to thank you in advance for taking the time to help me out.

I am using the following code to put an image on the screen:
----------------------
CGRect myImageRect = CGRectMake(xPosition,yPosition,320.0f,320.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:@"SimpleAnima01.png"]];
myImage.opaque= YES;
[self.view addSubview:myImage];
[myImage release];
----------------------

This works fine, however it doesn't have an Outlet so I can't 'talk' to it. I would like to be able to animated the image, to add a 'tapped' or 'drag' event and to be able to remove it again.

I have to do it in code because the data for the names and placement of the images comes from a textfile and is always different.

Any ideas?

Thank you so much.
Amanda521 is offline   Reply With Quote
Old 08-16-2010, 06:14 AM   #2 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 30
Reena Mehta is on a distinguished road
Default

Quote:
Originally Posted by Amanda521 View Post
Hi, I'm new to this forum (and the Iphone SDK) and want to thank you in advance for taking the time to help me out.

I am using the following code to put an image on the screen:
----------------------
CGRect myImageRect = CGRectMake(xPosition,yPosition,320.0f,320.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:@"SimpleAnima01.png"]];
myImage.opaque= YES;
[self.view addSubview:myImage];
[myImage release];
----------------------

This works fine, however it doesn't have an Outlet so I can't 'talk' to it. I would like to be able to animated the image, to add a 'tapped' or 'drag' event and to be able to remove it again.

I have to do it in code because the data for the names and placement of the images comes from a textfile and is always different.

Any ideas?

Thank you so much.
Declare your imageview object in .h file
Code:
UIImageView *myImage
__________________
Thanks
Reena Mehta is offline   Reply With Quote
Old 08-16-2010, 06:20 AM   #3 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 15
Amanda521 is on a distinguished road
Default

Quote:
Originally Posted by Reena Mehta View Post
Declare your imageview object in .h file
Code:
UIImageView *myImage
I don't know how many images will be on the screen when the app starts. I have to generate them all from a list. I could hardcode 10 images, and use them but then I'd be stuck with a maxiumum of 10. I'd like to create, talk to and remove them on the fly.

Thank you for your reponse.
Amanda521 is offline   Reply With Quote
Old 08-16-2010, 06:47 AM   #4 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,896
harrytheshark is on a distinguished road
Default

You could set tags, or add them to an NSMutableArray to keep track of them.
harrytheshark is offline   Reply With Quote
Old 08-16-2010, 07:11 AM   #5 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 15
Amanda521 is on a distinguished road
Default

Quote:
Originally Posted by harrytheshark View Post
You could set tags, or add them to an NSMutableArray to keep track of them.
I've been learning from the 'Lynda essential sdk training' and unfortunately it doesn't go into a lot of depth.

I suppose when I create a UI element, in this case a UIImage, in code it does have name, just like a variable. So can I talk to it using that name? Add a gesture recognizer and so on? I mean, I don't really need an IBOutlet I supopse.
Amanda521 is offline   Reply With Quote
Old 08-16-2010, 09:32 AM   #6 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 15
Amanda521 is on a distinguished road
Default

I've been trying a lot of things but can't get anything to work, I could use some more help.

So my app runs a bit of code if a button is pushed that puts an Image on the screen called 'myImage'. This works fine.

But I want to talk to the image, for example to remove it. If I have another button that sets 'myImage.image=nil'; I get a compiler error that 'myImage' is undeclared. Makes sense. But then how do I write a piece of code that talk to 'myImage' ? Can anyone give me an example.

Thank you so much.
Amanda521 is offline   Reply With Quote
Old 08-16-2010, 09:39 AM   #7 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 15
Amanda521 is on a distinguished road
Default

I also can't seem to use an NSString to store the name of the Image. When I try to make another method that is run when a button is clicked that says :

NSString *Temp=@"myImage";
Temp.image = nil;

My code doesn't compile because apparently that is an illegal statement. I am used to another scripting language where I would sat eval(temp+".image")=nil;.

I could really use some help, I am such a beginner that it's hard to find anything in the documentation.
Amanda521 is offline   Reply With Quote
Old 08-16-2010, 09:40 AM   #8 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,896
harrytheshark is on a distinguished road
Default

The way I would do it would be like:
Code:
[myImage setTag:123];
Then to call it back:
Code:
UIImageView * myImage = (UIImageView *)[self.view viewWithTag:123];
harrytheshark is offline   Reply With Quote
Old 08-16-2010, 10:56 AM   #9 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 15
Amanda521 is on a distinguished road
Default

Quote:
Originally Posted by harrytheshark View Post
The way I would do it would be like:
Code:
[myImage setTag:123];
Then to call it back:
Code:
UIImageView * myImage = (UIImageView *)[self.view viewWithTag:123];
Can you give me an example of how I would use that to set the alpha for example?

Thanx so much for helping me get started.
Amanda521 is offline   Reply With Quote
Old 08-16-2010, 11:20 AM   #10 (permalink)
iPhone Developer
 
Join Date: Jul 2009
Location: Eugene, Oregon
Posts: 97
williamsbm2000 is on a distinguished road
Default

Quote:
Originally Posted by Amanda521 View Post
Can you give me an example of how I would use that to set the alpha for example?

Thanx so much for helping me get started.
If you have a UIImage object that you have created programmatically as was suggested, you can set the alpha (or any other properties) like so:

Code:
[myImage setAlpha:0];
__________________
Apps available on the app store:
X-Plode!
williamsbm2000 is offline   Reply With Quote
Old 08-16-2010, 02:02 PM   #11 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 15
Amanda521 is on a distinguished road
Default

Quote:
Originally Posted by williamsbm2000 View Post
If you have a UIImage object that you have created programmatically as was suggested, you can set the alpha (or any other properties) like so:

Code:
[myImage setAlpha:0];
Thank you, but that seems to only work for me if I call it from the same method.

I want to be able to do the same things to this Image that I can do with one I created in ImageBuilder and connected up to an Outlet. And from any method, not just the one I create it in. So far I have not been able to find a way how to do that. Later I also want to add a listener to it to tell me if it has been tapped or dragged.

So far my only option is to create a bunch of Images that are invisible, connect them up and use them as I need them. I'd much rather create everything on the fly though and remove them again when I am done with them.
Amanda521 is offline   Reply With Quote
Old 08-16-2010, 02:19 PM   #12 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,896
harrytheshark is on a distinguished road
Default

I gave you in my example a way to access it from another method.
harrytheshark is offline   Reply With Quote
Old 08-16-2010, 02:36 PM   #13 (permalink)
iPhone Developer
 
Join Date: Jul 2009
Location: Eugene, Oregon
Posts: 97
williamsbm2000 is on a distinguished road
Default

If you declare a variable inside a method, the scope of that variable is only that method. To be able to access it from a different method, you need to declare it somewhere else, such as the .h file as was previously suggested and then handle accessing it like harrytheshark posted.

So you would just place the declaration in the head file:
Code:
UIImage *myImage;
and then in the method where you are currently declaring the object, you can initialize it rather than declaring it.

Code:
myImage = [UIImage imageNamed:@"image.png"];
Or something along those lines.. I am not near a computer at the moment so the syntax may not be exactly correct, but hopefully you understand what I'm trying to say. Good luck!
__________________
Apps available on the app store:
X-Plode!

Last edited by williamsbm2000; 08-16-2010 at 02:41 PM.
williamsbm2000 is offline   Reply With Quote
Old 08-16-2010, 03:53 PM   #14 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 15
Amanda521 is on a distinguished road
Default

Thank you for taking out the time to help me.

I don't have just one image, I could have hundreds, that depends on the text file that my app needs to interpret. Pre declaring 100's of UiImage variables seems like not such a smart move. Although maybe if they are not actually initialized with any data they won't take so much ram?!

I never really thought of the UIImage as a varable. In the end, even if the method that put it on the screen is never run again, the image is still there on the screen. So is the image on the screen just pixels in the screenbuffer, or is it still something that can be talked to, say if I wanted to change the alpha. It should be, how else am I supposed to do anything to it? I'm not doing a clearscreen to remove it :P

I'm really quite confused

Thanx again for taking out time to help me understand this.
Amanda521 is offline   Reply With Quote
Old 08-16-2010, 03:54 PM   #15 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 15
Amanda521 is on a distinguished road
Default

Quote:
Originally Posted by harrytheshark View Post
I gave you in my example a way to access it from another method.
Thank you, I'm fiddling around with it right now.
Amanda521 is offline   Reply With Quote
Old 08-16-2010, 04:03 PM   #16 (permalink)
iPhone Developer
 
Join Date: Jul 2009
Location: Eugene, Oregon
Posts: 97
williamsbm2000 is on a distinguished road
Default

The UIImage is a pointer to the actual image file which would be stored on the device, so if you declare two UIImages that point to the same file, you are not necessarily taking up a whole lot more memory. You can handle multiple UIImages by storing them in an NSArray, or an NSMutableArray if you would like to be able to add/remove a different number of objects at different times. The documentation is a great place to learn how to use an array if you are not already familiar with it.
__________________
Apps available on the app store:
X-Plode!
williamsbm2000 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting 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



» Advertisements
» Online Users: 353
4 members and 349 guests
givensur, linkmx, Newbie123, PlutoPrime
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,118
Posts: 402,894
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:48 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0