播放动画图像视图时的内存警告

时间:2013-11-24 23:02:19

标签: ios animation uiimageview

我有以下代码用图像播放动画:

-(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;
});

每次调用此函数时,内存都会增加10 MB。播放动画图像视图的好方法是什么?

1 个答案:

答案 0 :(得分:1)

每次调用此方法时,都会在视图中添加新的图像视图(及其图像数组)。您应该重构代码,以便创建数组和imageView在if子句中,测试imageView是否为nil,以便该部分仅在第一次调用方法时运行(并使imageView成为属性)。