4

I am recieving memory warning using 100 of animating images so I tried to use Core Animation instead but that gives me the same problem. This is because I don't know how to use replaceSublayer in my current code

UIView* upwardView=[[UIView alloc]init];
[upwardView setFrame:CGRectMake(0, 0, 1024, 768)];
[self.view addSubview:upwardView];

NSArray *animationImages=[NSArray arrayWithObjects:[UIImage imageNamed:@"001.png"],[UIImage imageNamed:@"001.png"],[UIImage imageNamed:@"002.png"],[UIImage imageNamed:@"003.png"],....,nil];

CAKeyframeAnimation *animationSequence = [CAKeyframeAnimation animationWithKeyPath: @"contents"];
animationSequence.calculationMode = kCAAnimationLinear;
animationSequence.autoreverses = YES;
animationSequence.duration = 5.00;
animationSequence.repeatCount = HUGE_VALF;

NSMutableArray *animationSequenceArray = [[NSMutableArray alloc] init];
for (UIImage *image in animationImages)
{
    [animationSequenceArray addObject:(id)image.CGImage];
}
CALayer *layer = [upwardView layer];
animationSequence.values = animationSequenceArray;
[layer addAnimation:animationSequence forKey:@"contents"];
  • http://stackoverflow.com/questions/442076/method-for-animating-images-like-a-movie-on-iphone-without-using-mpmovieplayer i have seen this ans. bt how to use it dnt know –  Sep 23 '13 at 08:47
  • 1
    I wouldn't expect the code above to be better in any way. You are still going to read a lot of images into memory. – David Rönnqvist Sep 23 '13 at 09:15
  • @DavidRönnqvist- how can i save memory usage than...? –  Sep 23 '13 at 12:29
  • See https://stackoverflow.com/questions/442076/method-for-animating-images-like-a-movie-on-iphone-without-using-mpmovieplayer#answer-6077394 – MoDJ Mar 20 '15 at 20:37
  • Are you not reading that array of images into memory twice? I don't think that the CGImage is a reference, I think it makes a copy in memory. Couldn't you just add the CGImage reference to the array once, instead of looping through them twice? – Vincil Bishop Sep 23 '16 at 21:51

1 Answers1

0

I guess you need to add a few lines more. Just replace the last three lines and add the following line.

    //Prepare CALayer
    CALayer *layer = [CALayer layer];
    layer.frame = self.view.frame;
    layer.masksToBounds = YES;
    [layer addAnimation:animationSequence forKey:@"contents"];
    [upwardView.layer addSublayer:layer]; // Add CALayer to your desired view

For detail implementation check this reference

Asif Newaz
  • 557
  • 7
  • 17