problem with animationImages array - iphone

here is my code :
-(void) createNewImage {
image.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"oeufgrisé.png"],
[UIImage imageNamed:#"abouffer_03.png"],
[UIImage imageNamed:#"boutonplay_03.png"],
[UIImage imageNamed:#"boutonpause_03.png"],
[UIImage imageNamed:#"abouffer_05.png"],
[UIImage imageNamed:#"mangeurcentremieu3_03.png"],nil];
[image setAnimationRepeatCount:10];
image.animationDuration =1.5;
[image startAnimating];
imageView = [[UIImageView alloc] initWithImage:image];
}
This code doesn't work, I don't know why. I have always this warning on line "imageView = [[UIImageView alloc] initWithImage:image];" :Incompatible ObjectiveC type struct UIImageView*' expected struct UIImage*' when passing argument 1 of initWithImage from distinct Objective C type

You can set animation images to UIImageView , and not to UIImage.
-(void) createNewImage {
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,460)];
imageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"oeufgrisé.png"],
[UIImage imageNamed:#"abouffer_03.png"],
[UIImage imageNamed:#"boutonplay_03.png"],
[UIImage imageNamed:#"boutonpause_03.png"],
[UIImage imageNamed:#"abouffer_05.png"],
[UIImage imageNamed:#"mangeurcentremieu3_03.png"],nil];
[imageView setAnimationRepeatCount:10];
imageView.animationDuration =1.5;
[imageView startAnimating];
[NSTimer scheduledTimerWithTimeInterval:15.0 target:self selector:#selector(setLastImage:) userInfo:nil repeats:NO];
}
-(void)setLastImage:(id)obj
{
[imageView performSelectorOnMainThread:#selector(setImage:) withObject:[UIImage imageNamed:#"mangeurcentremieu3_03.png"] waitUntilDone:YES];
}

If you are doing:
image.animationImages = [NSArray arrayWithObjects:
then image is an UIImageView (review the code where you allocate it). That is the reason why you get the error, since initWithImage expects an object of type UIImage:
- (id)initWithImage:(UIImage *)image
Either you did not mean image to be an image view, or possibly you don't need to allocate a second UIImageView in your method and can just use image.

Make sure that image is a UIImageView and not a UIImage. You can only create animationImages for UIImageViews.

Related

I want to apply animation on UIImageView(Created through UIBuilder)

I have added a UIView, On that UIImageView using UIBuider,
Now I want to apply animation on that imageView, so I'v taken an array of images & adding that
array to imageView through this code
NSArray *animationArray=[[NSArray alloc]init];
animationArray=[NSArray arrayWithObjects:
[UIImage imageNamed:#"rabbit1.png"],
[UIImage imageNamed:#"rabbit2.png"],
[UIImage imageNamed:#"rabbit3.png"],
[UIImage imageNamed:#"rabbit4.png"],
[UIImage imageNamed:#"rabbit5.png"],
[UIImage imageNamed:#"rabbit6.png"],
[UIImage imageNamed:#"rabbit7.png"],
[UIImage imageNamed:#"rabbit8.png"],
[UIImage imageNamed:#"rabbit9.png"],
[UIImage imageNamed:#"rabbit10.png"],
[UIImage imageNamed:#"rabbit11.png"],
[UIImage imageNamed:#"rabbit12.png"],
[UIImage imageNamed:#"rabbit13.png"], nil];
tempImageView.animationImages=animationArray;
tempImageView.animationDuration=2;
tempImageView.animationRepeatCount=0;
[tempImageView startAnimating];
tempImageView is created using UIBuider
But it is not woking, isn't showing anything.
There seems to a bug in the story board auto layout..i found the same no result..
Try it via code this works..
mainImageView = [[UIImageView alloc]init];
NSArray *animationArray = [[NSArray alloc]init];
animationArray = [NSArray arrayWithObjects:[UIImage imageNamed:#"first.png"],[UIImage imageNamed:#"second.png"],[UIImage imageNamed:#"first.png"],[UIImage imageNamed:#"second.png"],[UIImage imageNamed:#"first.png"],[UIImage imageNamed:#"second.png"],[UIImage imageNamed:#"first.png"],[UIImage imageNamed:#"second.png"],[UIImage imageNamed:#"first.png"],[UIImage imageNamed:#"second.png"], nil]; //add your images here
[mainImageView setFrame:CGRectMake(50,50,100,100)];
mainImageView.animationImages = animationArray; //mainImageView is imageview
mainImageView.animationDuration = 2;
mainImageView.animationRepeatCount = 0;
[mainImageView startAnimating];
[self.view addSubview:mainImageView];

Is it possible to animate an UIImage?

I'm wondering if there's anyway to animate an UIImage.
I know that UIImageViews are possible to animate but is there any way to do it directly in a UIImage.
Maybe with Open GL ES or something?
Or are there any other ways you can animate an MPMediaItemArtwork?
Thanks in advance!
Create a UIImageView and set the property of animationImages to an array of UIImages
Here is an example:
NSArray *animationFrames = [NSArray arrayWithObjects:
[UIImage imageWithName:#"image1.png"],
[UIImage imageWithName:#"image2.png"],
nil];
UIImageView *animatedImageView = [[UIImageView alloc] init];
animatedImageView.animationImages = animationsFrame;
[animatedImageView startAnimating];
If you're targeting iOS 5 you can do this directly in UIImage without the UIImageView using
+(UIImage *)animatedImageWithImages:(NSArray *)images duration:(NSTimeInterval)duration
for example,
[UIImage animatedImagesWithImages:animationFrames duration:10];
If you mean animation with a few images, use this code:
// create the view that will execute our animation
UIImageView* YourImageView = [[UIImageView alloc] initWithFrame:self.view.frame];
// load all the frames of our animation
YourImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"01.gif"],
[UIImage imageNamed:#"02.gif"],
[UIImage imageNamed:#"03.gif"],
[UIImage imageNamed:#"04.gif"],
[UIImage imageNamed:#"05.gif"],
[UIImage imageNamed:#"06.gif"],
[UIImage imageNamed:#"07.gif"],
[UIImage imageNamed:#"08.gif"],
[UIImage imageNamed:#"09.gif"],
[UIImage imageNamed:#"10.gif"],
[UIImage imageNamed:#"11.gif"],
[UIImage imageNamed:#"12.gif"],
[UIImage imageNamed:#"13.gif"],
[UIImage imageNamed:#"14.gif"],
[UIImage imageNamed:#"15.gif"],
[UIImage imageNamed:#"16.gif"],
[UIImage imageNamed:#"17.gif"], nil];
// all frames will execute in 1.75 seconds
YourImageView.animationDuration = 1.75;
// repeat the animation forever
YourImageView.animationRepeatCount = 0;
// start animating
[YourImageView startAnimating];
// add the animation view to the main window
[self.view addSubview:YourImageView];
Source
Check UIImage's +animatedImageWithImages:duration: and +animatedImageNamed:duration:.
From UIImage Class Reference:
Creates and returns an animated image from an existing set of images.
This method loads a series of files by appending a series of numbers to the base file name provided in the name parameter. For example, if the name parameter had ‘image’ as its contents, this method would attempt to load images from files with the names ‘image0’, ‘image1’ and so on all the way up to ‘image1024’. All images included in the animated image should share the same size and scale.
You can use this.
arrayOfImages=[[NSMutableArray alloc]init];
for(int i=1;i<=54;i++)
{
NSString *nameResources=#"haKsequence.png";
NSString *changedString= [NSString stringWithFormat:#"%d", i];
NSString *stringWithoutSpaces = [nameResources stringByReplacingOccurrencesOfString:#"K" withString:changedString];
[arrayOfImages addObject:(id)[UIImage imageNamed:stringWithoutSpaces].CGImage];
}
self.view.userInteractionEnabled=NO;
i1.hidden=YES;
image1=[[UIImageView alloc]init];
image1.backgroundColor=[UIColor clearColor];
image1.frame = button.frame;//i1 is obshaped buttion
[self.view addSubview:image1];
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:#"contents"];
animation.calculationMode = kCAAnimationDiscrete;
animation.duration = 1;
animation.values = arrayOfMonkeyImages;
[animation setDelegate:self];
animation.repeatCount=3;
[animation setRemovedOnCompletion:YES];
[image1.layer addAnimation:animation forKey:#"animation"];
Swift 4 version:
let animationImages = [UIImage(named: "image1.png"), UIImage(named: "image2.png")]
imageView.animationImages = animationImages
imageView.animationDuration = 10
imageView.startAnimating()

Freeze last frame of animation new code?

I'm trying to freeze the last frame of an animation. The following code used to work, however after an update to iOS it no longer works. How can I write this code so that the last image stays on the screen?
animation.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"watermelondrop2.png"],
[UIImage imageNamed:#"watermelondrop4.png"],
[UIImage imageNamed:#"watermelondrop6.png"],
[UIImage imageNamed:#"watermelondrop8.png"],
[UIImage imageNamed:#"watermelondrop10.png"],
[UIImage imageNamed:#"watermelondrop12.png"],
[UIImage imageNamed:#"watermelondrop14.png"],
[UIImage imageNamed:#"watermelondrop15.png"], nil];
animation.animationDuration = .8;
animation.animationRepeatCount = 1;
[animation startAnimating];
[self playwatermelondropsound];
[self.view addSubview:animation];
animation.image = [UIImage imageNamed:#"watermelondrop16.png"];
I spend 40 my minutes to resolve your problem, and it was just so simple:
There are two way you can achieve what you want:
Set the image you want on your screen as default image.
or just do this,
//make your last line to first line.
animation.image = [UIImage imageNamed:#"watermelondrop16.png"];
animation.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"watermelondrop2.png"],
[UIImage imageNamed:#"watermelondrop4.png"],
[UIImage imageNamed:#"watermelondrop6.png"],
[UIImage imageNamed:#"watermelondrop8.png"],
[UIImage imageNamed:#"watermelondrop10.png"],
[UIImage imageNamed:#"watermelondrop12.png"],
[UIImage imageNamed:#"watermelondrop14.png"],
[UIImage imageNamed:#"watermelondrop15.png"], nil];
animation.animationDuration = .8;
animation.animationRepeatCount = 1;
[animation startAnimating];
[self playwatermelondropsound];
[self.view addSubview:animation];
And guess what it'll start working, as I have tested it. :)
[self cleanStance];
NSArray *imageArray = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"jump1.png"],
[UIImage imageNamed:#"jump2.png"],
nil];
self.player.animationImages = imageArray;
self.player.animationDuration = 0.3;
//self.player.contentMode = UIViewContentModeBottomLeft;
[self.view addSubview:self.player];
[self.player startAnimating];
- (void)cleanStance {
[self.player setImage:nil];
self.player.animationImages = nil;
}
This is the example to show the image jumping i thick this may help u
Your code looks fine to me, except when I do it, I usually set the .image property of the UIImageView BEFORE I tell the UIImageView to start animating. Also, add [UIImage imageNamed:#"watermelondrop16.png"] to the end of your .animationImages array. Maybe that will fix your problem. Hope that Helps!

Trouble accessing objects in NSArray using randNum

I need to access an NSArray and get a value from it with:
-(IBAction) randClicked:(id)sender
{
int randNum;
//when user clicks the button
//create random number, 1 - 32
randNum = arc4random() %32;
As you see, I can display my random number here:
///using this code for testing random number when button is clicked
NSString *randValText = [[NSString alloc]initWithFormat:#"%d", randNum];
//Output random number to label
labelRandText.text = randValText;
//Call method seeImage with SEL methodSelector
SEL methodSelector = #selector(seeImage);
///set image opacity to 0
wisdomView.alpha = 0.0;
//Use NSTimer to call method
[NSTimer scheduledTimerWithTimeInterval:0 target:self selector:methodSelector userInfo:nil repeats:NO];
[randValText release];
}
But now, I need to compare the value of randNum and access an NSArray of images for it's position in the array so I can display an image in an UIImageView.
Having a difficult time trying to figure this one out. All help is greatly appreciated. Thank you for your time.
Here is the current code that I have implemented with help from Josh as of 11:13pm 7/6/2001. I think I almost have it...but something I'm doing is not working. I keep getting the warning "Method '-objectAtIndex not found (return type defaults to 'id')
//Call method seeImage with SEL methodSelector
SEL methodSelector = #selector(seeImage);
//Use NSTimer to call method
[NSTimer scheduledTimerWithTimeInterval:0 target:self selector:methodSelector userInfo:[NSNumber numberWithInt:randNum] repeats:NO];
[randValText release];
}
////This is the method to make the graphic choosen by randNum to display at the top of the screen
-(IBAction) seeImage: (NSTimer *)tim
{
CGContextRef imageContext = UIGraphicsGetCurrentContext();
int randNum = [(NSNumber *)[tim userInfo] intValue];
UIImage *imgToDisplay = [wisdomView objectAtindex:randNum];
wisdomView.image = imgToDisplay;
wisdomView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"0.png"],
[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed:#"3.png"],
[UIImage imageNamed:#"4.png"],
[UIImage imageNamed:#"5.png"],
[UIImage imageNamed:#"6.png"],
[UIImage imageNamed:#"7.png"],
[UIImage imageNamed:#"8.png"],
[UIImage imageNamed:#"9.png"],
[UIImage imageNamed:#"10.png"],
[UIImage imageNamed:#"11.png"],
[UIImage imageNamed:#"12.png"],
[UIImage imageNamed:#"13.png"],
[UIImage imageNamed:#"14.png"],
[UIImage imageNamed:#"15.png"],
[UIImage imageNamed:#"16.png"],
[UIImage imageNamed:#"17.png"],
[UIImage imageNamed:#"18.png"],
[UIImage imageNamed:#"19.png"],
[UIImage imageNamed:#"20.png"],
[UIImage imageNamed:#"21.png"],
[UIImage imageNamed:#"22.png"],
[UIImage imageNamed:#"23.png"],
[UIImage imageNamed:#"24.png"],
[UIImage imageNamed:#"25.png"],
[UIImage imageNamed:#"26.png"],
[UIImage imageNamed:#"27.png"],
[UIImage imageNamed:#"28.png"],
[UIImage imageNamed:#"29.png"],
[UIImage imageNamed:#"30.png"],
[UIImage imageNamed:#"31.png"],nil];
wisdomView.alpha = 0;
[UIView beginAnimations:nil context:imageContext];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:4];
[UIView setAnimationDelegate:self];
wisdomView.alpha = 1;
[UIView commitAnimations];
SEL methodSelector = #selector(hideImage);
[NSTimer scheduledTimerWithTimeInterval:4 target:self selector:methodSelector userInfo:nil repeats:NO];
}
I appreciate the help. Thank you for your time.
Here is the changed code that still doesn't work....I'm not understanding something. 1:19PM 7/7/2011
-(IBAction) seeImage:(NSTimer *)tim
{
NSArray *wisdom;
wisdom = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"0.png"],
[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed:#"3.png"],
[UIImage imageNamed:#"4.png"],
[UIImage imageNamed:#"5.png"],
[UIImage imageNamed:#"6.png"],
[UIImage imageNamed:#"7.png"],
[UIImage imageNamed:#"8.png"],
[UIImage imageNamed:#"9.png"],
[UIImage imageNamed:#"10.png"],
[UIImage imageNamed:#"11.png"],
[UIImage imageNamed:#"12.png"],
[UIImage imageNamed:#"13.png"],
[UIImage imageNamed:#"14.png"],
[UIImage imageNamed:#"15.png"],
[UIImage imageNamed:#"16.png"],
[UIImage imageNamed:#"17.png"],
[UIImage imageNamed:#"18.png"],
[UIImage imageNamed:#"19.png"],
[UIImage imageNamed:#"20.png"],
[UIImage imageNamed:#"21.png"],
[UIImage imageNamed:#"22.png"],
[UIImage imageNamed:#"23.png"],
[UIImage imageNamed:#"24.png"],
[UIImage imageNamed:#"25.png"],
[UIImage imageNamed:#"26.png"],
[UIImage imageNamed:#"27.png"],
[UIImage imageNamed:#"28.png"],
[UIImage imageNamed:#"29.png"],
[UIImage imageNamed:#"30.png"],
[UIImage imageNamed:#"31.png"],nil];
int randNum = [(NSNumber *)[tim userInfo] intValue];
CGContextRef imageContext = UIGraphicsGetCurrentContext();
UIImage *imgToDisplay = [wisdom objectAtIndex:randNum];
wisdomView.image = imgToDisplay;
Still getting the Method '-objectAtIndex:' not found warning and now '' may not respond to '' 'NSArray' may not respond to '-objectAtIndex'. Arrrrgggg.
Thank you for your time.
Tuck the generated number into the timer's user info slot:
[NSTimer scheduledTimerWithTimeInterval:0
target:self
selector:#selector(seeImage:)
userInfo:[NSNumber numberWithInt:randNum]
repeats:NO];
and then get it back out in the timer's method. By the way, timer methods are supposed to have one parameter.* The timer passes itself to the method for just such a case as this, i.e., when you need to send some info along to be used when it fires.
- (void) seeImage: (NSTimer *)tim {
// Retrieve the NSNumber, using a cast because userInfo returns id
int randNum = [(NSNumber *)[tim userInfo] intValue];
NSImage * imgToDisplay = [arrayOfImgs objectAtIndex:randNum];
// and so on...
}
*Although they do work with any number of parameters, including 0.
Try:
[imageArray objectAtIndex:randNum]
EDIT: based on the additional info in the comments, I think #Josh Caswell's response better answers the question.
//create random number, 1 - 32
randNum = arc4random() %32;
that would give you 0 - 31. check your array bounds.
I fixed it! Thanks Josh for the assist!
-(IBAction) seeImage
{
int randNum;
//when user clicks the button
//create random number, 0 - 31
randNum = arc4random() %32;
NSString *randValText = [[NSString alloc]initWithFormat:#"%d", randNum];
//Output random number to label - testing code
//labelRandText.text = randValText;
///set image opacity to 0
wisdomView.alpha = 0.0;
wisdom = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"0.png"],
[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed:#"3.png"],
[UIImage imageNamed:#"4.png"],
[UIImage imageNamed:#"5.png"],
[UIImage imageNamed:#"6.png"],
[UIImage imageNamed:#"7.png"],
[UIImage imageNamed:#"8.png"],
[UIImage imageNamed:#"9.png"],
[UIImage imageNamed:#"10.png"],
[UIImage imageNamed:#"11.png"],
[UIImage imageNamed:#"12.png"],
[UIImage imageNamed:#"13.png"],
[UIImage imageNamed:#"14.png"],
[UIImage imageNamed:#"15.png"],
[UIImage imageNamed:#"16.png"],
[UIImage imageNamed:#"17.png"],
[UIImage imageNamed:#"18.png"],
[UIImage imageNamed:#"19.png"],
[UIImage imageNamed:#"20.png"],
[UIImage imageNamed:#"21.png"],
[UIImage imageNamed:#"22.png"],
[UIImage imageNamed:#"23.png"],
[UIImage imageNamed:#"24.png"],
[UIImage imageNamed:#"25.png"],
[UIImage imageNamed:#"26.png"],
[UIImage imageNamed:#"27.png"],
[UIImage imageNamed:#"28.png"],
[UIImage imageNamed:#"29.png"],
[UIImage imageNamed:#"30.png"],
[UIImage imageNamed:#"31.png"],nil];
CGContextRef imageContext = UIGraphicsGetCurrentContext();
//pick image randNum index position
wisdomView.image = [wisdom objectAtIndex:randNum];
wisdomView.alpha = 0.0;
[UIView beginAnimations:nil context:imageContext];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:4];
[UIView setAnimationDelegate:self];
wisdomView.alpha = 1;
[UIView commitAnimations];
SEL methodSelector = #selector(hideImage);
[NSTimer scheduledTimerWithTimeInterval:4 target:self selector:methodSelector userInfo:nil repeats:NO];
[randValText release];
}
Thank you for your time!

How Can I Use UISlider to cycle through an UIImageView image sequence to create an animated effect

For the app i am currently working on i am trying to take a series of images and link them to a UISlider. Interacting with the slider will create an animated effect, cycling through the frames. I have been able to create a standalone animation from the series of images, however i am not too certain about how to add the UISlider and make the appropriate links to achieve the desired effect.
Here is what i have so far...
- (void) viewDidLoad{
//---Animate Images Array----
/*
NSArray *images = [NSArray arrayWithObjects:
[UIImage imageNamed:#"frame1.png"],
[UIImage imageNamed:#"frame2.png"],
[UIImage imageNamed:#"frame3.png"],
[UIImage imageNamed:#"frame4.png"],
[UIImage imageNamed:#"frame5.png"],
[UIImage imageNamed:#"frame6.png"],
[UIImage imageNamed:#"frame7.png"],
[UIImage imageNamed:#"frame8.png"],
[UIImage imageNamed:#"frame9.png"],
[UIImage imageNamed:#"frame10.png"],
nil];
CGRect frame = CGRectMake(0,44,703,704);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
imageView.animationImages = images;
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.animationDuration = 4;
imageView.animationRepeatCount = 0;
[imageView startAnimating];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:imageView cache:YES];
[self.view addSubview:imageView];
[imageView release];
[super viewDidLoad];
Any clues?
I don't think you can get a UISlider object to work with animationImages property of an UIImageView. However you can maintain an array of UIImages and then set the UIImageView object's image property. For this you will have to set an action for the UIControlEventValueChanged event. Set the max value of the slider to the number of images i.e. [imageArray count] and then based on the current value of the slider, update the image.
- (void)valueChanged:(UISlider *)slider {
NSInteger currentImageIndex = lroundf(slider.value);
imageView.image = [imageArray objectAtIndex:currentImageIndex];
[slider setValue:roundf(slider.value) animated:YES];
}