Burak
Burak

Reputation: 5764

Memory Warning while playing animated image view

I have the following code to play animation with images:

-(void)playPopAnimation:(void (^)(void))completion{

__block NSMutableArray *images;
float animationDuration = 1.0f;
images = [[NSMutableArray alloc] initWithCapacity:26];
    for(int i=1; i<26; i++){
        NSString *fileName = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"bulut_pop%d",i] ofType:@"png"];
        UIImage *image = [UIImage imageWithContentsOfFile:fileName];
        //UIImage* image = [UIImage imageNamed:[NSString stringWithFormat:@"bulut_pop%d.png",i]];
        [images addObject:image];
    }
[Helper playSound:@"button_pop"];    
UIImageView* imageView = [[UIImageView alloc] initWithFrame:self.itemImage.frame];
imageView.animationImages = images;
imageView.animationDuration = animationDuration;
imageView.animationRepeatCount = 1; // 0 = nonStop repeat
[self addSubview:imageView];
[imageView startAnimating];

self.imageButton.userInteractionEnabled = NO;
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, animationDuration * NSEC_PER_SEC), dispatch_get_current_queue(), ^{
    //[self.delegate myItemCell:self cellDidTappedFor:[self item]];
    self.imageButton.userInteractionEnabled = YES;
    images = nil;
});

Each time I call this function, memory increases by 10 MB. What is the good way to play animated image views?

Upvotes: 0

Views: 360

Answers (1)

rdelmar
rdelmar

Reputation: 104082

Each time you call this method, you add a new image view (with its array of images) to your view. You should refactor your code so that the creation of the array and imageView are inside an if clause testing whether imageView is nil, so that part only runs the first time the method is called (and make imageView a property).

Upvotes: 1

Related Questions