xcoder
xcoder

Reputation: 1436

UIImageView animation lag

I have a problem with the animation. At start, everything is smooth. However, when the application runs for a certain period of time (around 10 min) then the animation lags.

Here is the code in viewDidLoad:

[NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(updateImage) userInfo:nil repeats:YES];

And in -(void)updateImage: //image fade in

[aSubview release];
[aSubview removeFromSuperview];
aSubview = [[UIImageView alloc] initWithFrame:CGRectMake(28, 64, 265, 284)];
aSubview.image = [slideShowArray objectAtIndex:randNum];
[aSubview setAlpha:0.0];
[UIImageView beginAnimations:NULL context:nil];
[UIImageView setAnimationDuration:2.0];
[aSubview setAlpha:1.0];
[UIImageView commitAnimations];
[self.view addSubview:aSubview];

UIImageView *film = [[UIImageView alloc] initWithFrame:CGRectMake(20, 34, 280, 344)];
film.image = [UIImage imageNamed:@"film5.png"];
[film setAlpha:1.0];
[self.view addSubview:film];
[film release];

Can anyone explain this? Is it due to memory issue or something else? Thanks in advance.

Upvotes: 0

Views: 806

Answers (3)

taskinoor
taskinoor

Reputation: 46037

[self.view addSubview:aSubview];

You are adding a subview in every 3 seconds. So after 10 mins there are 200 subviews which requires too much memory and thus slowing the app. Before adding a new subview, remove the previous ones if they are not needed. And if you need 200 subviews simultaneously then you should reconsider your design.

EDIT: After the edit of the question, why do you need to add a film every time? You are adding a new film object every time the method is called. Note that, super view retains subviews. So if the method is called 20 times then you have 20 film objects in memory.

Upvotes: 2

Jhaliya - Praveen Sharma
Jhaliya - Praveen Sharma

Reputation: 31730

[self.view addSubview:aSubview];

The above is not the issue at all, from the apple documentation for addSubView .

Views can have only one superview. If view already has a superview and that view is not the receiver, this method removes the previous superview before making the receiver its new superview.

Animation is always a heavy operation in term of memory that need to store many thing before and after animation, which are not visualable to as application developer. And in your case you are doing it more than 10 min.. in a loop that lead to reduce the availability of memory for next animation operation.

Upvotes: 0

Cyprian
Cyprian

Reputation: 9453

Yes, your problem is with memory aggregating over time. To solve this problem use Instruments to check memory usage and find out what causes it. You might not release some object. In XCode go to Product -> Profile or press "cmd+i" choose "Allocations" and add "Leaks" manualy. Run Instruments and try to debug your problem.

Upvotes: 0

Related Questions