How should I go about animating over 200 images in

2019-05-07 15:12发布

I have over 200 images i want to animate over the space of around 10 seconds. I have tried using animationImages by loading the images into an array and then calling the startAnimating method. This worked okay in the simulator but crashed the iPad.

I have tried calling an NStimer every 1/25th of a second and changing the image each time the timer fired. This performed better than the previous method, it worked well in the simulator but also crashed near the end of the (laggy) animation on the iPad.

Can someone help me out and tell me the ideal way of approaching this problem? Thanks.

ORIGINAL CODE:

- (void) humptyFallingAnim {

    NSString *filename;
    if (humptyImageCounter < 285) {
        filename = [NSString stringWithFormat:@"Humpty Animation HD1.2 png sequence/humpty_HD1.2_%d.png", humptyImageCounter];
            UIImage *someImage = [UIImage imageNamed:filename];
            humptyFalling.image = someImage;
            NSLog(@"loaded image: %d", humptyImageCounter);
        humptyImageCounter++;
    } else {
        NSLog(@"Timer invalidated");
        [humptyTimer invalidate];
        humptyTimer = nil;
    }

}

EDIT: Some new code that isn't working for me

NSString *filename;
    if (humptyImageCounter < 285) {
        filename = [NSString stringWithFormat:@"Humpty Animation HD1.2 png sequence/humpty_HD1.2_%d.png", humptyImageCounter];
        @autoreleasepool {
            UIImage *someImage = [UIImage imageWithContentsOfFile:filename];
            humptyFalling.image = someImage;
            NSLog(@"loaded image: %d", humptyImageCounter);
        }
        humptyImageCounter++;
    } else {
        NSLog(@"Timer invalidated");
        [humptyTimer invalidate];
        humptyTimer = nil;
    }

EDIT 2:

-(void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    NSLog(@"You shook it!");
    [self.view bringSubviewToFront:imgSnail];
    if (deviceHasBeenShaken == 0) {
        deviceHasBeenShaken = 1;
    }
    humptyTimer = [NSTimer scheduledTimerWithTimeInterval:(1/25) target:self selector:@selector(humptyFallingAnim) userInfo:nil repeats:YES];
    [self moveHumptyPosition];

}

4条回答
Ridiculous、
2楼-- · 2019-05-07 15:36

Use cocos2d my friend. Save yourself the headache. What you are talking about is a hundred times more efficiently done with OpenGLES. However, OpenGL is unnecessary complicated, and cocos2d provides a good abstraction layer over it so that you can just do it with a few lines of code.

查看更多
等我变得足够好
3楼-- · 2019-05-07 15:48

Unfortunately, this sounds like a job for OpenGL ES. Is the app a game?

查看更多
4楼-- · 2019-05-07 15:51

Try using CALayer implicit animation. Create your layers and set their images in a loop, like this:

NSMutableArray *allLayers = [NSMutableArray array];
for (int i = 0 ; i != 200 ; i++) {
    CGImageRef image = ...; // Get i-th image from an image source
    CALayer* sub = [CALayer layer];
    sub.contents = (__bridge id)image;
    sub.contentsRect = CGRectMake(...); // Create a rectangle based on the image size
    [self.layer addSublayer:sub];
    [allLayers addObject:sub];
}

Now you can start animating your layers implicitly by setting their properties:

for (CALayer *sub in allLayers) {
    sub.position = CGMakePoint(...); // The position to which to move the image
}

Once you are done with the layers, remove them from superview:

for (CALayer *sub in allLayers) {
    [sub removeFromSuperlayer];
}
查看更多
▲ chillily
5楼-- · 2019-05-07 15:57

Memory management :).

NSString* somePath;
UIImageView *imageView;
for (i=0;i<200;i++) {
@autoreleasepool {
UIImage *someImage = [UIImage imageWithContentsOfFile:somePath];
imageView.image = someImage; //every time this line is executed, old reference disappears and previous UIImage is discarded
}
}

I mean, you have to implement autoreleasepools and don't use +(UIImage *) imageNamed: method, because it caches images.

UPDATE Let me explain. Your problem is memory management related. Your app has to use device's memory efficiently but your actually doesn't. I don't know how your app works, but general idea is shown above. UPDATE 2 let me know whether you use ARC or not.

UPDATE 3 sorry for my bizarre language but it's late. Forgive me :)

查看更多
登录 后发表回答