images from an array - objective-c

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

Related

ImageArray Assignment to single Image

I am new to iphone development. I have an Image Array as follows.
NSArray *eyeFrames = [NSArray array];
eyeFrames = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"eyes1-open.png"],
[UIImage imageNamed:#"eyes3-half.png"],
[UIImage imageNamed:#"eyes2-empty.png"],
nil];
I have an ImageView as follows
IBOutlet UIImageView *eyesImage;
Now I want to assign this array of Images to my ImageView .
What i tried so far is as follows:
eyesImage.animationImages = eyeFrames;
But it doesn't seem to provide the expected result.Can anyone tell me how to do it ?
Thanks,
Raj
For animating series of images like image slider following is the code:
NSArray *myImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"eyes1-open.png"],
[UIImage imageNamed:#"eyes3-half.png"],
[UIImage imageNamed:#"eyes2-empty.png"],
nil];
eyesImage.animationImages = myImages;
eyesImage.animationDuration = 0.9; // seconds
eyesImage.animationRepeatCount = 1; // 0 = loops forever
[eyesImage startAnimating];
[UIView commitAnimations];
For accessing the file from array and set to Image view you have to do like this
IBOutlet UIImageView *eyesImage = [[UIImageView alloc] initWithImage:(UIImage *) [eyeFrames objectAtIndex:1]];
Hope this helps...
Don`t forget to
set "animationDuration" and "animationRepeatCount" properties, then call "startAnimating"
eyesImage.animationDuration=0.1; //seconds
eyesImage.animationRepeatCount=2;
[eyesImage startAnimating];

view images from array's index

i created an array that contain images, i animate them via animationImages. but now i want to view image from the array index(i want to create a for loop with count, and if my count is equal to 1 so it will view the image at index 1 from the array) and animate them. i need it becuase i want to view number from 1 to 100 and if my number is equal to 11 then i will show number 1 twich(to avoid from creating 100 image of numbers).
how can i do it?
here is my correct 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 = [NSArray arrayWithObjects:numberZero, numberOne, numberTwo, numberThree, numberFour, numberFive, numberSix, numberSeven, numberEight, numberNine, nil];
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];
thanks!
I'm a little unsure of your question, but I gather you're trying to animate counting to 100(?) using your images as the digits. Thus, use one UIImageView for each digit, all with the same animations images, and animate the different places at relative durations:
float onesDurations = 2;
UIImageView *onesPlace = [[UIImageView alloc] init...];
onesPlace.animationImages = numbersArray;
onesPlace.animationDuration = onesDurations;
UIImageView *tensPlace = [[UIImageView alloc] init...];
tensPlace.animationImages = numbersArray;
tensPlace.animationDuration = 10*onesDurations; // ten times as slow as ones animation
UIImageView *hundredsPlace = [[UIImageView alloc] init...];
hundredsPlace.animationImages = numbersArray;
hundredsPlace.animationDuration = 100*onesDurations; // 100 times as slow animation
// add them all to the view
// start their animations
You could display text instead of images with a custom font. If you want to add a border or outline, then you will have to do abit more work. this question explains how you can add an outline to your text.
hope this helps

How to display image one by one when taking array?

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

CABasicAnimation with 5 images

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

layer of animation images in iphone

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];