模拟动画耗尽内存

时间:2011-03-05 20:21:35

标签: iphone image ipad uiimage nstimer

我正在尝试基于一系列54张图像模拟动画。我得到一个 - “程序接收信号:”0“。”消息 - 内存不足。

我正在尽力照顾他们,我似乎失败了。我在大约4-5秒内完成所有54个,然后它应该重复(几乎像翻书)。由于图像中有光泽的3D材质,动画不是可以使用Core动画轻松完成的。

或者,是否有更好的方法来解决这个问题?

以下是代码:

-(void)addImage:(NSString*)imageName{
    if (self.images == nil){
        self.images = [[[NSMutableArray alloc] init] autorelease];
    }
    [self.images addObject:[UIImage imageNamed:imageName]];
}

// Returns a UIImageView that contains the next image to display
- (UIImageView *)nextImageView
{
    // get the image at the next index
    UIImage *image = [images objectAtIndex:pictureIndex];
    ++pictureIndex; // increment the index

    if(pictureIndex > images.count -1){
        pictureIndex = 0;
    }


    // create an image view for the image
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

    // resize the image to fill the screen without distorting
    imageView.frame = rotator.frame;

    imageView.autoresizesSubviews = NO;
    [imageView setContentMode:UIViewContentModeCenter];


    // Makes the image move proportionally in any direction if the
    // bounds of the superview change when the iPhone is rotated.
    imageView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin |
                                  UIViewAutoresizingFlexibleRightMargin |                          
                                  UIViewAutoresizingFlexibleTopMargin |                            
                                  UIViewAutoresizingFlexibleBottomMargin);

    return imageView;
}

- (void)timerFired:(NSTimer *)_timer{
    UIImageView *nextImageView = [self nextImageView];
    if(nextImageView){
        [self.view addSubview:nextImageView];
        nextImageView.alpha = 0.0;

        //NSLog(@"timerFired - image no: %i", pictureIndex);

        // begin animation block
        [UIView beginAnimations:nil context:nextImageView];
        [UIView setAnimationDuration:animationDuration]; // set the animation length
        [UIView setAnimationDelegate:self]; // set the animation delegate

        // call the given method when the animation ends
        [UIView setAnimationDidStopSelector:@selector(transitionFinished:finished:context:)];

        // make the next image appear with the chosen effect
        [nextImageView setAlpha:1.0]; // fade in the next image
        [currentImageView setAlpha:0.0]; // fade out the old image

        [UIView commitAnimations];
    } else {
        [timer invalidate];
        timer = nil;
    }

}

// called when the image transition animation finishes
- (void)transitionFinished:(NSString *)animationId finished:(BOOL)finished context:(void *)context
{
    [currentImageView removeFromSuperview]; // remove the old image
    [currentImageView release]; // release the memory for the old image
    currentImageView = context; // assign the new image
}

-(void)startRotator{
    self.rotator.animationImages = self.images;
    self.rotator.animationDuration = animationDuration;

    pictureIndex = 0; // reset the index

    currentImageView = [self nextImageView]; // load the first image
    [self.view addSubview:currentImageView]; // add the image to the view

    //[self.rotator startAnimating];
    NSLog(@"start Animating - Image Count: %i", self.images.count);

    timer = [NSTimer scheduledTimerWithTimeInterval:scheduledInterval 
                                             target:self
                                           selector:@selector(timerFired:) 
                                           userInfo:nil 
                                            repeats:YES];
}

1 个答案:

答案 0 :(得分:0)

您在imageView中泄露nextImageView。返回的实例应该是自动释放的。

你确定你的54张照片符合记忆吗?