I have a simple for loop which changes the images in a set of 9 UIImageViews. However, I have one problem, I can't change the name of the UIImageView in co-ordination of the for loop and so the end result is that the for loop ONLY affects 1 out of 9 UIImageViews I have in my iOS App.
Here is my code:
for (current_photo = 1; current_photo < 10; current_photo++) {
picview_1.image = [UIImage imageNamed:[NSString stringWithFormat:#"%#%i.png", SERVER_URL, current_photo]];
}
You will notice that in my code there is a bit:
picview_1.image
When I replace the "pic view_1" which "picview_current_photo", it comes up with errors.
What am I doing wrong? Please explain.
Thanks for your time :)
Create an array of UIImageViews, and access them in a loop through an index, like this:
NSArray *imageViews = #[picview_1, picview_2, picview_3, picview_4, ..., picview_9];
// Arrays are indexed from zero, so I changed the index to run from 0 to 9
for (current_photo = 0; current_photo < 9; current_photo++) {
// Since current_photo is zero-based, I added one to it in stringWithFormat
UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:#"%#%i.png", SERVER_URL, current_photo+1]];
[[imageViews objectAtIndex:current_photo] setImage:img];
}
You are probably best to set up an array of UIImageViews. Then access the UIImageView that you need from that array and set the image as required.
Related
I found a way to store images of UIImagePickerControllerSourceTypePhotoLibrary in an NSArray but I can not find a solution to view images in an UIImageView from the NSArray. Here is the link to store image:
Can the selected images from library be stored in an array?
You just need to iterate through that array...
for(UIImage *img in self.imageArray){
self.image = img;
}
Or imageView:
for(UIImageView *imgView in self.imageArray){
self.imageView = imgView;
}
Lets say you stored images in array :
NSArray *imageArray = #[image1, image2, image3]; //imageX are arbitrary name for images.
To view these images one by one, you need to load them and show in UIImageView:
//lets assume you have multiple buttons and all targeted to this method.
-(IBAction)buttonClicked:(UIButton *)sender{
NSInteger index = sender.tag;
//here index is the integer to load the image.
if(index < imageArray.count){
self.imageView setImage: imageArray[index];
}
}
I have picked up programming as a hobby, so please bear with any 'old school' or completely wrong practices!
I am trying to move three images across the view using a timer. They then need to be dodged by another UIImageView. The obstacles are in an array:
objArray = [NSMutableArray arrayWithObjects:[UIImage imageNamed:#"img-1.png"],
[UIImage imageNamed:#"img-2.png"],
[UIImage imageNamed:#"img-3.png"], nil];
I then have a for loop to create these views, and it's here that I think the problems start.
count = [objArray count];
for(int i=0; i < count; i++)
{
NSLog (#"Element %i = %#", i, [objArray objectAtIndex: i]);
randX = arc4random_uniform(450);
randX = randx + 50;
randY = arc4random_uniform(236);
randY = randy + 45;
imgObj = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[objArray objectAtIndex:i]]];
imgObj.center = CGPointMake(randX, randY);
[self.view addSubview:imgObj];
[self startAnimation];
All three images display ok. The problem is getting these to move.
I understand the need for the timer, which is initiated in the startAnimation method. I have tried setting a tag in the for loop. However this always results in the last object "img-3.png", which then moves ok.
My question is: how do I differentiate the three views I create in the loop so I can call them elsewhere?
Assigning tags to you UIImageViews should work:
imgObj.tag = i+1; //skipping 0 here since other subviews may have this tag
Then you should be able to access the views via
[self.view viewWithTag:tag];
Or you could create another NSMutableArray and store the UIImageViews in it.
I'm currently making a game. I have an UIImageView that scrolls from right to left and once it leaves the ViewController the image resets on the right and scrolls left again, this currently set to displays "image1" I would like to change it to display a different image randomly from a set of 5 each time it resets to the right.
Here is my Method:
- (void)PlacePipe{
RandomTopPipePosition = arc4random() %350;
RandomTopPipePosition = RandomTopPipePosition - 228;
RandomBottomPipePosition = RandomTopPipePosition + 660;
PipeTop.center = CGPointMake(340-10, RandomTopPipePosition);
randomImagebottom.center = CGPointMake(340-10, RandomBottomPipePosition);
}
The name of the images I want to randomly add into this UIImageView are -toppipestyleone.png, toppipestyletwo.png, toppipestylethree.png, toppipestylefour.png, toppipestylefive.png".
I'm not sure at the best route to doing this, I looked at doing it with an array but I'm not sure how to set up an array or even call images randomly.
You could put the image names in an array as you considered, ex.
NSArray *imageNameArray = [[NSArray alloc] initWithObjects:#"toppipestyleone.png", #"toppipestyletwo.png", #"toppipestylethree.png", #"toppipestylefour.png", #"toppipestylefive.png", nil];
then create and set an image using the image name at a random index of that array using:
imageView.image = [UIImage imageNamed:[imageNameArray objectAtIndex:arc4random_uniform((uint32_t)[imageNameArray count])];
I need to take the current value of mtype and pass it forward to Mselect so that the image pushed forward is the same as the one rotating in the animation.
here is the code sample
- (void)viewDidLoad
{
//Array to hold Images
NSMutableArray *imageArray = [[NSMutableArray alloc] initWithCapacity:Mush_Count];
for (mtype = 1; mtype < Mush_Count; mtype++)
{
[imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:#"Mush%i.png", mtype]]];
//make button
SelectBt.imageView.animationImages = [NSArray arrayWithArray:imageArray];
SelectBt.imageView.animationDuration = 5;
[SelectBt.imageView startAnimating];
Mselect = mtype;
}
}
-(IBAction)Selection:(id)sender{
[self Placement];
}
-(void)Placement{
if (Place1Occ == NO) {
[self Place1];
}
}
-(void)Place1{
if (Place1Occ == NO) {
Place1.image =[UIImage imageNamed:[NSString stringWithFormat:#"Mush%i.png", Mselect]];
Place1Occ = YES;
}
}
The animation loops just fine and the selection of the images works but it's not selecting the image the is currently on the screen it selects the last image on the Array.
Any suggestions?
for (mtype = 1; mtype < Mush_Count; mtype++)
{
[imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:#"Mush%i.png", mtype]]];
}
That loop is enough to build the array of images - you don't need to set the imageView's animation images until this loop completes and the array is populated.
As it stands, you reset the animation images each loop and set Mselect to type each iteration: this is why you always get the last image index stored in Mselect
This code should be after the for loop:
SelectBt.imageView.animationImages = [NSArray arrayWithArray:imageArray];
SelectBt.imageView.animationDuration = 5;
[SelectBt.imageView startAnimating];
As far as I know, you can't get the current frame of the animation directly - you have to create a timer when you start the animation and have this count the number of frames (by increasing your Mselect value. Then use that to load the current image
Is SelectBT a subclass of UIButton? if so is it the (id)sender coming from your IBAction?
If so then you can just grab the image directly from the SelectBT instance by calling sender.imageView.image
As an aside, objective-C convention is to capitalize ClassNames, but start instances with lowerCase like this ClassName *instanceOfClassName and you're likely to take flack for it around here if you don't adhere to that convention
Update
Could you bump your imageArray into a class variable or a property? It already contains all the fully loaded UIImages, which you can get back out by calling [imageArray objectAtIndex:i]
Then you just have to keep track of which numeric index is facing your user, or which index they tapped and pull the corresponding image.
Feel free to post your whole class if you'd like us to explain more.
I'm creating a UIImageView within the code and I want to get it later on and set an image. How do I do this please help?
for (int x = 1; x <= 5; x++) {
UIImageView *Game_ImageViews = [[UIImageView alloc]
initWithFrame:CGRectMake(0, 0, 100, 100)];
Game_ImageViews.tag = x;
[Game_View addSubview:Game_ImageViews];
}
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:#"Test.png"]];
(UIImageView*)[self.view viewWithTag:100].image = image;
[image release];
viewWithTag will give you the view with the tag if a subview with that tag exists. So for example, your self.view would have to have a subview that has a tag of 100.
Initially, you are setting up subviews of Game_View with tags of 1, 2, 3, 4, 5.
So, to retrieve a subview by tag you would have to do, assuming you want to retrieve the view tagged by id 1:
((UIImageView*)[Game_View viewWithTag:1]).image = image;
A couple of other things worth mentioning. You're not releasing Game_ImageViews in your loop above, so that will give a memory leak.
Also, if you use the imageNamed method of UIImage, that will give you a couple of benefits. First, you'll have a cached image which could improve performance. Secondly, you'll have an autoreleased object which saves you the bother of releasing it yourself. Unless of course you want maximum performance and control by doing it as you have done above.