2

I do lot of animations in my iOS app using UIImageView.animationImages by passing an array of UIImage objects to the method listed below for animation. Current my array of UIImage objects contains separate PNG files. I know I can do similar animations by using & passing a Sprite Sheet instead of passing an array of separate PNG files. What are the advantages of one over another or Sprite Sheets are meant for a different purpose?

- (void) startImageViewAnimation:(UIImageView *)imageView WithImages:(NSArray*)imageArray orBuildArrayWithImage:(NSString *)imageName duration:(CGFloat)duration repeatCount:(NSInteger)repeatCount soundFile:(NSString *)soundFile stopSoundAfterDuration:(CGFloat) stopSoundDuration
{

    if([imageName length] != 0)
    {
        NSMutableArray *array = [[NSMutableArray alloc] init];
        UIImage *image;
        int index = 1;
        NSString *string;
        do
        {
            string = [NSString stringWithFormat:@"%@%02d.png", imageName, index++];
            image = [UIImage imageNamed:string];
            if(image)
                [array addObject:image];
        } while (image != nil);

        imageView.animationImages = array;
        [array release];
    }
    else
    {
        imageView.animationImages = imageArray;
    }
    imageView.animationDuration = duration;
    imageView.animationRepeatCount = repeatCount;
    [imageView startAnimating];

    if([soundFile length] != 0)
    {
        [self loadSoundEffectAudio:soundFile];
    }
}
Alex
  • 833
  • 3
  • 15
  • 28

1 Answers1

0

Sprite Sheets are only useful for very small animations where all the frames of the animation can fit into 1 "sheet" that is loaded into memory and then diff x,y offsets are used to access different frame data. If you have a longer animation or one that is larger, then sprite sheets are of no value at all. Back in the day, character sprites were rather small so a lot of them could fit on one sheet and then a short animation like a punch could be animated quickly because no memory would need to get swapped in from one frame to the next. For anything but the smallest of small animations, you should not be using UIImageView.animationImages because it will run out of memory and crash your device when used with even medium size animations of reasonable size. More detailed info about better approaches can be found in my answer to this question How to do animations using images efficiently in iOS

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