I am beginner of iphone, I have all images in array but how to display one by one when I execute the array in a loop..
NSArray *images = [[NSArray arrayWithObjects:
[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed:#"3.png"],
[UIImage imageNamed:#"4.png"],
nil] retain];
int i=0;
int width = 0;
int height = 0;
for ( NSString *imageName in images )
{
//NSTimer *timer=[[NSTimer alloc]init];
UIImage *image = [images objectAtIndex:i];
//UIImage *image = [UIImage imageNamed:imageName];
animalphoto = [[UIImageView alloc] initWithImage:image];
animalphoto.contentMode = UIViewContentModeScaleAspectFit;
animalphoto.clipsToBounds = YES;
animalphoto.animationDuration=5.0;
animalphoto.animationRepeatCount=0;
animalphoto.frame = CGRectMake( animalphoto.frame.size.width * i++, 0, animalphoto.frame.size.width, animalphoto.frame.size.height);
width = animalphoto.frame.size.width;
height = animalphoto.frame.size.height;
NSLog(#"print:%#",image);
[animalphoto release];
}
}
Give any suggestion and sample code which is apply in our code..
Instead of putting one image just put whole array of images to the imgview.animationImages = images without the for loop
For further reference visit this site.
you need to use UIImageView's object animationImages to set the UIImage array
set animationRepeatCount
set animationDuration
then call [imageView startAnimating]; to start the animation
Related
i found this code which is very helpful to avoid delay at first animation:
NSMutableArray *menuanimationImages = [[NSMutableArray alloc] init];
for (int aniCount = 1; aniCount < 21; aniCount++) {
NSString *fileLocation = [[NSBundle mainBundle] pathForResource: [NSString stringWithFormat: #"bg%i", aniCount + 1] ofType: #"png"];
// here is the code to pre-render the image
UIImage *frameImage = [UIImage imageWithContentsOfFile: fileLocation];
UIGraphicsBeginImageContext(frameImage.size);
CGRect rect = CGRectMake(0, 0, frameImage.size.width, frameImage.size.height);
[frameImage drawInRect:rect];
UIImage *renderedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[menuanimationImages addObject:renderedImage];
}
settingsBackground.animationImages = menuanimationImages;
But i want to use the same image sometimes - can i fill within the code above my array manually via
testArray = [[NSArray alloc] initWithObjects:[UIImage imageNamed:#"test_001.png"],
[UIImage imageNamed:#"test_002.png"],
[UIImage imageNamed:#"test_001.png"],
nil];
Thanks for ideas!
Yes you can by using this
UIImageView * animatedImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200,200)];
testArray = [[NSArray alloc] initWithObjects:[UIImage imageNamed:#"test_001.png"], [UIImage imageNamed:#"test_002.png"],[UIImage imageNamed:#"test_001.png"],nil];
animTime =3.0;
[animatedImageView setAnimationImages:testArray] ;
animatedImageView.animationDuration = animTime;
animatedImageView.animationRepeatCount = 1;
[self.view addSubview: animatedImageView];
[animatedImageView startAnimating];
i create an nsmutableArray and i add for it an images. also i create an imageView that i want to show the images from the array, i tried to use animationImages but i nothing is happened, how can i fix it?
here is my code:
//Numbers images
UIImage *numberZero = [UIImage imageNamed:#"numberZero.png"];
UIImage *numberOne = [UIImage imageNamed:#"numberOne.png"];
UIImage *numberTwo = [UIImage imageNamed:#"numberTwo.png"];
UIImage *numberThree = [UIImage imageNamed:#"numberThree.png"];
UIImage *numberFour = [UIImage imageNamed:#"numberFour.png"];
UIImage *numberFive = [UIImage imageNamed:#"numberFive.png"];
UIImage *numberSix = [UIImage imageNamed:#"numberSix.png"];
UIImage *numberSeven = [UIImage imageNamed:#"numberSeven.png"];
UIImage *numberEight = [UIImage imageNamed:#"numberEight.png"];
UIImage *numberNine = [UIImage imageNamed:#"numberNine.png"];
//add numbers uiimage to numbersArray
numbersArray = [NSMutableArray arrayWithObjects:numberZero, numberOne, numberTwo, numberThree, numberFour, numberFive, numberSix, numberSeven, numberEight, numberNine, nil];
imgview1 = [[UIImageView alloc] init];
imgview1.animationImages = numbersArray;
imgview1.animationDuration = 2;
imgview1.animationRepeatCount=0;
[imgview1 startAnimating];
[imgview1 stopAnimating];
thanks!
You need to actually add imgview1 to something so that it displays. If imgview1 was created in Interface Builder then there is no need to assign. Also you stop your animation immediately after starting it.
//If created in IB comment out the next line
imgview1 = [[UIImageView alloc] init];
imgview1.animationImages = numbersArray;
imgview1.animationDuration = 2;
imgview1.animationRepeatCount=0;
//If not created in IB then add this to a view e.g:
[self.view addSubview:imgview1];
[imgview1 startAnimating];
//[imgview1 stopAnimating]; this is too soon to stop animating
I assume that by "nothing happened" you mean that you see the first image, but the sequence does not move beyond that.
This is probably because you stopAnimating too soon: it does not even get a chance to start!
i solve the problem, i guess the problem were because i didn't use init with frame to my imgview.
here is the final code:
UIImageView *imgview1 = [[UIImageView alloc] initWithFrame:CGRectMake(40, 90, 240, 240)];
imgview1.animationImages = numbersArray;
imgview1.animationDuration = 2;
imgview1.animationRepeatCount=1;
[imgview1 startAnimating];
[self.view addSubview:imgview1];
hope u understand...
I have 8 UIImageView in my view, all declared in the .h and .m.
I called them img01, img02, img03,..., img08
and I also have 8 pics, named 01.png, 02.png, 03.png,..., 08.png.
if I want to set the first imageView to the first pic I'll use the following command:
UIImage *image = [UIImage imageNamed:#"01.png"];
[img01 setImage:image];
What if I have a lot of UIImageViews and pics?
I tried to use this code but couldn't figure out how to do the last line of code.
int count = 1;
while (count <= 8) {
NSString *countString = [[NSString alloc]initWithFormat:#"%i", count];
NSString *picname = [NSString stringWithFormat:#"0%#.png", countString];
NSString *imgName = [NSString stringWithFormat:#"img0%#", countString];
UIImage *image = [UIImage imageNamed:picname];
[img01 setImage:image];
count++;
}
The code will only change the first imageView. I need it to change all the imageViews.
Create a NSMutableArray, and fill it with the UIImageViews. Currently you only set the first:
[img01 setImage:image];
You can do something like that:
NSMutbaleArray imageViews = [[NSMutableArray alloc] initWithCapacity:8];
for (int i = 0; i < 8; ++i) {
NSString *countString = [NSString stringWithFormat:#"%i", count];
NSString *picname = [NSString stringWithFormat:#"0%#.png", countString];
NSString *imgName = [NSString stringWithFormat:#"img0%#", countString];
UIImage *image = [UIImage imageNamed:picname];
UIImageView * view = [[[UIImageView alloc] init] autorelease];
[view setImage:image];
[imageViews addObject:view];
}
how to change this code to make the animation like this:
image1 --> image2 ---> image3 --->image4 --->image 5 .. then return back to image1 and so on ...
the code:
UIImage *image1 = [UIImage imageNamed:#"1.jpg"];
UIImage *image2 = [UIImage imageNamed:#"2.jpg"];
UIImage *image3 = [UIImage imageNamed:#"3.jpg"];
UIImage *image4 = [UIImage imageNamed:#"4.jpg"];
UIImage *image5 = [UIImage imageNamed:#"5.jpg"];
NSArray *imageArray = [NSArray arrayWithObjects:image1.CGImage, image2.CGImage, image3.CGImage, nil];
//self.introImages.image = image1;
[self.view addSubview:self.introImages];
CABasicAnimation *crossFade = [CABasicAnimation animationWithKeyPath:#"contents"];
crossFade.autoreverses = YES;
crossFade.repeatCount = HUGE_VALF;
crossFade.duration = 1.0;
//self.introImages.layer.contents = image2.CGImage;
crossFade.fromValue = (id)image1.CGImage;
crossFade.toValue = (id)image5.CGImage;
[self.introImages.layer addAnimation:crossFade forKey:#"animateContents"];
Better a late answer then no one i think. :)
There are several mistakes in your code.
First of all if you want to use crossfade of images you should add the animation to the imageView containing the images and just switch between the UIImages.
Second: If you want to cycle through more images than two, you should either use a queue of animations with the help of animationDidFinish callback methods, or you should use the animationImages field of the UIImageView class to let the UIImageView do all the work.
Third: If you are changing the layer of the image from image1 to image5 how could you expect the compiler to know that it should put all the other pics in between?
Try this code:
UIImage *image1 = [UIImage imageNamed:#"1.jpg"];
UIImage *image2 = [UIImage imageNamed:#"2.jpg"];
UIImage *image3 = [UIImage imageNamed:#"3.jpg"];
UIImage *image4 = [UIImage imageNamed:#"4.jpg"];
UIImage *image5 = [UIImage imageNamed:#"5.jpg"];
NSArray *imageArray = [NSArray arrayWithObjects:image1.CGImage, image2.CGImage, image3.CGImage, nil];
//imageView to add the array of images you want to cycle through
iv_introImages.animationImages = imageArray;
//duration of the animation-cycle
iv_introImages.animationDuration = 1.0;
//how often you want the animation to repeat. 0 stands for endless repetition
iv_introImages.animationRepeatCount = 0;
//starts the animation
[iv_introImages startAnimating];
hope this helps.
Mav
How I can animate two sets of images one on the other on xcode? I mean that one set is background or landscape and the other set is a figure in the background
Here's a basic approach you might try:
UIImageView* backdropScene = [[UIImageView alloc] initWithFrame:self.view.frame];
backdropScene.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"rainy_background01.png"],
[UIImage imageNamed:#"rainy_background02.png"],
[UIImage imageNamed:#"rainy_background03.png"],
[UIImage imageNamed:#"rainy_background04.png"], nil];
backdropScene.animationDuration = .2f; // image change rate, .2 seconds
backdropScene.animationRepeatCount = 0; // repeat infinitely
[self.view addSubview:backdropScene];
[backdropScene startAnimation];
UIImageView* foregroundAnimation = [[UIImageView alloc] initWithFrame:self.view.frame];
foregroundAnimation.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"man_walk01.png"],
[UIImage imageNamed:#"man_walk02.png"],
[UIImage imageNamed:#"man_walk03.png"],
[UIImage imageNamed:#"man_walk04.png"], nil];
foregroundAnimation.animationDuration = .5f; // image change rate, .2 seconds
foregroundAnimation.animationRepeatCount = 0; // repeat infinitely
[self.view addSubview:foregroundAnimation];
[foregroundAnimation startAnimation];