精灵表与用于动画的PNG文件阵列



我在iOS应用程序中使用UIImageView.animationImages,通过将一组UIImage对象传递到下面列出的方法来制作动画。当前我的UIImage对象数组包含单独的PNG文件。我知道我可以通过使用&传递Sprite Sheet,而不是传递一组单独的PNG文件。一个比另一个有什么优势,或者精灵床单有不同的用途?

- (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];
    }
}

Sprite Sheets仅适用于非常小的动画,其中动画的所有帧都可以放入加载到内存中的1个"sheet"中,然后使用diff x,y偏移来访问不同的帧数据。如果你有一个更长或更大的动画,那么精灵表就没有任何价值。以前,角色精灵很小,所以很多精灵都可以放在一张纸上,然后像一拳这样的短动画可以很快被动画化,因为不需要在一帧到下一帧之间交换内存。对于除最小的小动画外的任何动画,您都不应该使用UIImageView.animationImages,因为即使与大小合理的中等大小动画一起使用,它也会耗尽内存并导致设备崩溃。关于更好的方法的更多详细信息可以在我对这个问题的回答中找到。如何在iOS 中有效地使用图像制作动画

最新更新