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.
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.
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.
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.
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.
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.
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.
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.
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.
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!