sequencing image using core animation, Recieving memory warnings
Asked Answered
D

1

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"];
Duluth answered 23/9, 2013 at 8:42 Comment(5)
#442576 i have seen this ans. bt how to use it dnt knowDuluth
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.Seem
@DavidRönnqvist- how can i save memory usage than...?Duluth
See #442576Arluene
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?Rounded
O
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

Oregon answered 10/8, 2021 at 13:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.