【问题标题】:Sprit Sheets Vs array of PNG files for animations用于动画的精灵表与 PNG 文件数组
【发布时间】:2012-07-19 18:20:40
【问题描述】:

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

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

【问题讨论】:

    标签: ios uiimageview sprite-sheet


    【解决方案1】:

    精灵表仅适用于非常小的动画,其中动画的所有帧都可以放入加载到内存中的 1 个“表”中,然后使用 diff x,y 偏移量来访问不同的帧数据。如果您有更长的动画或更大的动画,那么精灵表就没有任何价值。过去,角色精灵相当小,因此其中很多可以放在一张纸上,然后可以快速制作像拳击这样的短动画,因为不需要将内存从一帧交换到下一帧。除了最小的小动画之外,你不应该使用 UIImageView.animationImages ,因为它会耗尽内存并在与合理大小的中等大小动画一起使用时使你的设备崩溃。更多关于更好方法的详细信息可以在我对这个问题的回答中找到How to do animations using images efficiently in iOS

    【讨论】:

      猜你喜欢
      • 2021-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 2017-06-21
      • 2019-03-10
      相关资源
      最近更新 更多