Hi i have a uislider that changes the images in uiimageview.At first time it works great there is no delay . But when i click the button to publish (there is a modal view that opens) and close the modal view (this is the second time) the uiimageview delays that shown the images.i cant find why it gets slower:(
-(IBAction)sliderSlide:(UISlider *)aSlider {
float f=slider.value;
NSString *show=[NSString stringWithFormat:#"%.0f %%",f];
label2.text=show;
NSString *show1=[NSString stringWithFormat:#"%.0f",f];
imageView.image = [UIImage imageNamed:[NSString stringWithFormat:#"/Smiley_000%#.png", show1]];
float slid=slider.value;
if(slid>99)
{
[imageView1 setHidden:NO];
[imageView2 setHidden:YES];
NSString *fullpath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:#"/Smiley_00099.png"];
imageView.image = [UIImage imageWithContentsOfFile:fullpath];
secondarray=[NSArray arrayWithObjects:
[UIImage imageNamed:#"Rays_00000.png"],
[UIImage imageNamed:#"Rays_00001.png"],
[UIImage imageNamed:#"Rays_00002.png"],
[UIImage imageNamed:#"Rays_00003.png"],
[UIImage imageNamed:#"Rays_00004.png"],
[UIImage imageNamed:#"Rays_00005.png"],
[UIImage imageNamed:#"Rays_00006.png"],
[UIImage imageNamed:#"Rays_00007.png"],
[UIImage imageNamed:#"Rays_00008.png"],
[UIImage imageNamed:#"Rays_00009.png"],
nil];
imageView1.animationImages = secondarray;
// How many seconds it should take to go through all images one time.
imageView1.animationDuration = 0.5;
// How many times to repeat the animation (0 for indefinitely).
imageView1.animationRepeatCount = 0;
[imageView1 startAnimating];
}
else if (slid<1)
{
[imageView2 setHidden:NO];
[imageView1 setHidden:YES];
NSString *fullpath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:#"/Smiley_0000.png"];
imageView.image = [UIImage imageWithContentsOfFile:fullpath];
firstarray=[NSArray arrayWithObjects:
[UIImage imageNamed:#"Teardrop_00000.png"],
[UIImage imageNamed:#"Teardrop_00001.png"],
[UIImage imageNamed:#"Teardrop_00002.png"],
[UIImage imageNamed:#"Teardrop_00003.png"],
[UIImage imageNamed:#"Teardrop_00004.png"],
[UIImage imageNamed:#"Teardrop_00005.png"],
[UIImage imageNamed:#"Teardrop_00006.png"],
[UIImage imageNamed:#"Teardrop_00007.png"],
[UIImage imageNamed:#"Teardrop_00008.png"],
[UIImage imageNamed:#"Teardrop_00009.png"],
[UIImage imageNamed:#"Teardrop_00000.png"],
[UIImage imageNamed:#"Teardrop_00000.png"],
[UIImage imageNamed:#"Teardrop_00000.png"],
[UIImage imageNamed:#"Teardrop_00000.png"],
[UIImage imageNamed:#"Teardrop_00000.png"],
nil];
imageView2.animationImages = firstarray;
// How many seconds it should take to go through all images one time.
imageView2.animationDuration = 0.8;
// How many times to repeat the animation (0 for indefinitely).
imageView2.animationRepeatCount = 0;
[imageView2 startAnimating];
[self.view addSubview:imageView2];
}
else
{
[imageView1 setHidden:YES];
[imageView2 setHidden:YES];
[imageView1 stopAnimating];
[imageView2 stopAnimating];
}
[[NSUserDefaults standardUserDefaults] setFloat:slid forKey:#"slider"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
this is my code if u can see why performance decrease in the second time i slide the slider pls tell me
edit 1:
i cancel some animations and slider get back to its speed but now the pictures are sticking slider okey but pictures are slow then the first time.
Well, I would say that your first step here to making this method more responsive is to remove all object allocations and unnecessary logic from this delegate method. The slider delegate method can be called over 100 times a second (if it is setup as continuous) and you should really be using it to only update ivars and perform minimal logic. This is most likely the root cause of your delays. As for why it is happening on the second time around, perhaps you are duplicating slider setup on your viewWillAppear, so after the modal, the delegate method is called twice as much.
My alertview makes the application (slider and images) goes slower so i remove it and it work just fine.
Related
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()
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!
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];
}
The code I wrote for 3.2 performs differently in OS4. Still no errors, just new bugs.
So I'm initializing the imageView inside an IBAction called "randomize" that produces and random number that goes to one of 86 cases producing a resulting animation and image, the only difference in code between 0 and 86 is that final imageView.image (I just didn't want to past 86 copies of the same code in for you to have to wade through ). No matter what the case, the animation imageView.animationImages runs. The bug is that the first time I run the action, imageView.animationImages runs showing my animation, then instead of completing the code and going to the imageView.image = [UIImage imageNamed:#"image36.png"], it now just shows whatever I have set as the actual imageView background in Interface Builder. And Sometimes it runs the animation for one case and the imageview.image for another. Any ideas?
- (IBAction)randomize {
int Number = arc4random() % 86;
switch (Number) {
case 0:
imageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"image1.png"],
[UIImage imageNamed:#"image2.png"],
[UIImage imageNamed:#"image3.png"],
[UIImage imageNamed:#"image4.png"],
[UIImage imageNamed:#"image5.png"],
[UIImage imageNamed:#"image6.png"],
[UIImage imageNamed:#"image7.png"],
[UIImage imageNamed:#"image8.png"],
[UIImage imageNamed:#"image9.png"],
[UIImage imageNamed:#"image10.png"],
[UIImage imageNamed:#"image0.png"],nil];
imageView.animationDuration = 0.50;
[imageView setAnimationRepeatCount: 1];
[imageView startAnimating];
imageView.image = [UIImage imageNamed:#"image36.png"];
break;
case 1:
imageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"image1.png"],
[UIImage imageNamed:#"image2.png"],
[UIImage imageNamed:#"image3.png"],
[UIImage imageNamed:#"image4.png"],
[UIImage imageNamed:#"image5.png"],
[UIImage imageNamed:#"image6.png"],
[UIImage imageNamed:#"image7.png"],
[UIImage imageNamed:#"image8.png"],
[UIImage imageNamed:#"image9.png"],
[UIImage imageNamed:#"image10.png"],
[UIImage imageNamed:#"image0.png"],nil];
imageView.animationDuration = 0.50;
[imageView setAnimationRepeatCount: 1];
[imageView startAnimating];
imageView.image = [UIImage imageNamed:#"image37.png"];
break;
If I had to guess, you're missing break statments at random intervals in your 86 cases. Please consider refactoring to a method...
Is there a way to freeze the last frame of my animation in my iphone app? Here's my code so far.
popup.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"picture1.png"],
[UIImage imageNamed:#"picture2.png"],
[UIImage imageNamed:#"picture3.png"],
[UIImage imageNamed:#"picture4.png"],
[UIImage imageNamed:#"picture5.png"],
[UIImage imageNamed:#"picture6.png"],
[UIImage imageNamed:#"picture7.png"], nil];
popup.animationDuration = 1.750567;
popup.animationRepeatCount = 1;
[popup startAnimating];
[self.view addSubview:popup];
When animation stops UIImageView shows UIImage set in its image property. So you should just set it to the last frame of your animation:
popup.image = [UIImage imageNamed:#"picture7.png"];