0

I have several animations which, when triggered, have varying lengths of (unintentional) delays before they execute.

Inside viewDidLoad i have something like:

NSString *fileName;
myArray = [[NSMutableArray alloc] init];
for(int i = 1; i < 285; i++) {
    fileName = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"HD1.2 png sequence/HD1.2_%d", i] ofType:@"png"];
    UIImage *image = [UIImage imageWithContentsOfFIle:fileName];
    [humptyArray addObject:image];
    //NSLog(@"Added object number %d: %@", i,regularImage);
}
falling.userInteractionEnabled = NO;
falling.animationImages = humptyArray;
falling.animationDuration = 6.3f;
falling.animationRepeatCount = 1;

I put in the NSLog so i could confirm that the array is populated with the images, which it is. When i want to trigger the animation i call [falling startAniamting]. Although the array has been preloaded with images there is still a delay between me triggering the animation and the animation executing.

What can i do so that there is no delay when i trigger the animation?

garethdn
  • 12,022
  • 11
  • 49
  • 83
  • You might not have enough RAM. UIImage will auotmagically deallocate images and read from disk again on the next draw if you load too many of them. – Abhi Beckert Sep 09 '12 at 18:46

2 Answers2

0
    @autoreleasepool {
            NSString *fileName;
            humptyArray = [[NSMutableArray alloc] init];
            dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
            dispatch_async(concurrentQueue, ^{   
            for(int i = 1; i < 285; i++) {
                fileName = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"HD1.2 png sequence/HD1.2_%d", i] ofType:@"png"];
                UIImage *image = [UIImage imageWithContentsOfFIle:fileName];
                [humptyArray addObject:image];
                //NSLog(@"Added object number %d: %@", i,regularImage);
            }
            falling.userInteractionEnabled = NO;
            falling.animationImages = humptyArray;
            falling.animationDuration = 6.3f;
            falling.animationRepeatCount = 1;
                dispatch_async(dispatch_get_main_queue(), ^{

                    [humptyArray release];

                });
            });
    }

Taken from my own answer in this other [SO question][1]


  [1]: http://stackoverflow.com/a/11364856/253008
shabbirv
  • 8,958
  • 4
  • 33
  • 34
  • Before i attempt this i should mention that i'm using ARC. Does this affect anything apart from leaving out the release statement? – garethdn Sep 09 '12 at 18:43
  • I gave that a try but to no avail, i still get better performance using an `NSTimer` than i do using `UIView Animation`, which i've read should NOT be the case – garethdn Sep 10 '12 at 10:12
0

You will find that people on SO suggest using animationImages, but it can eat up all your system memory and it is not fast. Please have a look at my answer to smooth-video-looping-in-ios, it is talking about looping video but the issue is the same. Please also take a look at my answer to iphone-smooth-transition-from-one-video-to-another. There are 2 example xcode projects available at these links that show an implementation that will start playing quickly and loop without any glitch all without eating up all your memory and causing an app crash.

Community
  • 1
  • 1
MoDJ
  • 4,309
  • 2
  • 30
  • 65