ImageArray Assignment to single Image - objective-c

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

Related

Can not adjust UIPopupController to display images

In my application (code listed below), I use a popover to display a series of colors that the user can choose. These colors are used for the color of the drawing they are completing above. I am trying to modify the popover to work the same way, except for this time I would want to display images (the images are saved in the application's documents folder as png files) instead of blocks of color. Listed below is the working code for the color selector popover. ColorGrid is a UIview which contains an NSArray Colors, as well as two NSUIntegers columnCount and rowCount. I have tried to replace the items in the colors array with UIImages of the png files, as well as UIImageViews but I have not been able to get a successful result (or a compilable one). Listed below is the working code. Could anyone show me how I can change the UIColor items to the images to show them in the grid?
- (IBAction)popoverStrokeColor:(id)sender {
StrokeColorController *scc = [[[StrokeColorController alloc] initWithNibName:#"SelectColorController" bundle:nil] autorelease];
scc.selectedColor = self.strokeColor;
[self doPopoverSelectColorController:scc sender:sender];
}
- (void)doPopoverSelectColorController:(SelectColorController*)scc sender:(id)sender {
[self setupNewPopoverControllerForViewController:scc];
scc.container = self.currentPopover;
self.currentPopover.popoverContentSize = scc.view.frame.size;
scc.colorGrid.columnCount = 2;
scc.colorGrid.rowCount = 3;
scc.colorGrid.colors = [NSArray arrayWithObjects:
//put the following below back in after testing
[UIColor blackColor],
[UIColor blueColor],
[UIColor redColor],
[UIColor greenColor],
[UIColor yellowColor],
[UIColor orangeColor],
//[UIColor purpleColor],
// [UIColor brownColor],
// [UIColor whiteColor],
// [UIColor lightGrayColor],
//[UIColor cyanColor],
//[UIColor magentaColor],
nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(colorSelectionDone:) name:ColorSelectionDone object:scc];
[self.currentPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; //displays the popover and anchors it to the button
}
Thanks for your help. I am new to objective-c.
edit - heres the function with my attempt to insert the images instead of the colors
- (void)doPopoverSelectColorController:(SelectColorController*)scc sender:(id)sender {
[self setupNewPopoverControllerForViewController:scc];
scc.container = self.currentPopover;
self.currentPopover.popoverContentSize = scc.view.frame.size;
// these have to be set after the view is already loaded (which happened
// a couple of lines ago, thanks to scc.view...
scc.colorGrid.columnCount = 2;
scc.colorGrid.rowCount = 3;
//here we need to get the UIImage items to try to put in the array.
NSArray *pathforsave = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [pathforsave objectAtIndex:0];
//here we need to add the file extension onto the file name before we add the name to the path
//[fileName appendString:#".hmat"];
NSString *strFile = [documentDirectory stringByAppendingPathComponent:#"test.png"];
NSString *strFile1 = [documentDirectory stringByAppendingPathComponent:#"test1.png"];
NSString *strFile2 = [documentDirectory stringByAppendingPathComponent:#"test2.png"];
NSString *strFile3 = [documentDirectory stringByAppendingPathComponent:#"test3.png"];
NSString *strFile4 = [documentDirectory stringByAppendingPathComponent:#"test4.png"];
NSString *strFile5 = [documentDirectory stringByAppendingPathComponent:#"test5.png"];
//now for the Images
UIImage *image = [ UIImage imageWithContentsOfFile: strFile];
UIImage *image1 = [ UIImage imageWithContentsOfFile: strFile1];
UIImage *image2 = [ UIImage imageWithContentsOfFile: strFile2];
UIImage *image3 = [ UIImage imageWithContentsOfFile: strFile3];
UIImage *image4 = [ UIImage imageWithContentsOfFile: strFile4];
UIImage *image5 = [ UIImage imageWithContentsOfFile: strFile5];
UIImageView *imageview = [[[UIImageView alloc] initWithImage:image] autorelease];
[self.view addSubview:imageview];
UIImageView *imageview1 = [[[UIImageView alloc] initWithImage:image1] autorelease];
[self.view addSubview:imageview1];
UIImageView *imageview2 = [[[UIImageView alloc] initWithImage:image2] autorelease];
[self.view addSubview:imageview2];
UIImageView *imageview3 = [[[UIImageView alloc] initWithImage:image3] autorelease];
[self.view addSubview:imageview3];
UIImageView *imageview4 = [[[UIImageView alloc] initWithImage:image4] autorelease];
[self.view addSubview:imageview4];
UIImageView *imageview5 = [[[UIImageView alloc] initWithImage:image5] autorelease];
[self.view addSubview:imageview5];
imageview.image = image;
imageview1.image = image1;
imageview2.image = image2;
imageview3.image = image3;
imageview4.image = image4;
imageview5.image = image5;
scc.colorGrid.colors = [NSArray arrayWithObjects:
// When attempting to add the images like this - get the error identified expected
// after the e in image, at the end bracket. Putting a * does nothing to change the error
[image],
// When adding one of the Imageviews, i get the same error as above
//below is how I attempted to add it
[imageView],
//
nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(colorSelectionDone:) name:ColorSelectionDone object:scc];
[self.currentPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; //displays the popover and anchors it to the button
}
Remove your square brackets around image and/or imageView :
scc.colorGrid.colors = [NSArray arrayWithObjects:
// Not : [image] but
image,
// or
imageView,
// Not : [imageView],
nil];

images from an array

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

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