简体   繁体   中英

Animating imageview takes time for first time

I am animating the ImageViews then the user taps a button. I have more than 40 images. The code I have used is

arr3 =  [[NSArray alloc]initWithObjects:[UIImage imageNamed:@"Aperture_00000.png"],
[UIImage imageNamed:@"Aperture_00001.png"],
[UIImage imageNamed:@"Aperture_00002.png"],
...
[UIImage imageNamed:@"Aperture_00023.png"], nil];

imgv.animationImages = arr3;
imgv.animationDuration=2.0f;
imgv.animationRepeatCount =1;

The method to start the animation is:

-(void)animate {
    [imgv startAnimating];
}

But it takes a lot of time when the user presses the button for the first time. What could be the solution for this?

The reason is following code:

arr3 =  [[NSArray alloc]initWithObjects:[UIImage imageNamed:@"Aperture_00000"],[UIImage imageNamed:@"Aperture_00001"],[UIImage imageNamed:@"1.png",@"2.png",@"3png",@"4.png",@"5.png",@"6.png",@"7.png",@"8.png",@"9.png",@"10.png",@"11.png",@"12.png",@"13.png",@"14.png",@"15.png",];

What you can do is load this array somewhere else.

Note : This is a very memory consuming way of loading the images. Your app will definitely crash after you visit this class 2-3 times. Instead of this use some alternative. The easiest alternative I can suggest is load a image on UIImageView and change the image periodically. It'll give you animation effect.

I would suggest you rather doing it programmatically you should create animated gif image from the images. Here is online tool you can set speed and other parameters http://picasion.com/

and use https://github.com/arturogutierrez/Animated-GIF-iPhone UIImageView category to display that gif image in that case you can save your CPU time.

Unhide the imageView when you want to play animation and hide when you want to stop.

I would suggest to give that at least a try.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM