不使用 MPMoviePlayer 在 iPhone 上动画图像(如电影)的方法 [英] Method for animating images (like a movie) on iPhone without using MPMoviePlayer

查看:18
本文介绍了不使用 MPMoviePlayer 在 iPhone 上动画图像(如电影)的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够在静态图像上显示动画.

I need to be able to display an animation over a static image.

鉴于 MPMoviePlayer 无法让您控制任何有用的东西,我能想到的唯一方法是使用多个静态图像,我们(一个接一个)显示这些图像以创建类似电影"的动画.

Given that MPMoviePlayer gives you no control over anything useful, the only way I can think to do this is to use multiple static images, which we display (one-by-one) to create a "movie like" animation.

我知道我们可以使用 UIImageView 来做到这一点(通过设置 UIImageView animationImages 属性然后调用 startAnimation),但是我们将有超过 100 个图像在我们的动画 - 所以内存使用将被最大化.

I know we could use UIImageView to do this (by setting the UIImageView animationImages property and then calling startAnimation), however we are going to have over 100 images in our animation - so memory usage will be maxed out.

有没有人有什么好的方法来制作这种动画?使用核心动画还是 OpenGL?

Does anyone have any nice ways to do this kind of animation? Using Core Animation or OpenGL?

我的猜测是我们需要创建一个图像缓冲区,当我们加载新图像时,我们会显示图像缓冲区中的图像??

My guess is that we'd need to create an image buffer, and as we load new images, we display images from the image buffer??

推荐答案

您可以使用 Core Animation CALayer 来托管您的动画,并在该主层内外交换一系列 CALayer 以逐帧执行动画片.您可以使用它的 contents 属性将托管图像帧的 CALayer 的内容设置为 CGImageRef.可以根据需要创建一系列包含图像的 CALayer 并将其存储在 NSMutableArray 中,然后在完成后将其删除以最大程度地减少内存使用.

You could use a Core Animation CALayer to host your animation, and swap a series of CALayers in and out of that main layer to perform your frame-by-frame animation. You can set the content of an image-frame-hosting CALayer to a CGImageRef using its contents property. A series of CALayers containing your images could be created and stored in an NSMutableArray as needed, then removed when done to minimize memory use.

您可以通过将 replaceSublayer:with: 方法调用包装在 CATransaction 中来设置帧之间的过渡持续时间,如下所示:

You can set the transition duration between frames by wrapping the replaceSublayer:with: method call in a CATransaction, like the following:

[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithFloat:0.25f] // 1/4th of a second per frame
                 forKey:kCATransactionAnimationDuration];   
[mainLayer replaceSublayer:[imageLayers objectAtIndex:oldImageIndex] with:[imageLayers objectAtIndex:newImageIndex]];
[CATransaction commit];

如果您的帧显示时间足够短,您也可以在主图层的内容中换入和换出 CGImageRef.

You might also be able to get away with swapping in and out the CGImageRef in the contents of your main layer, if your frame display time is short enough.

这篇关于不使用 MPMoviePlayer 在 iPhone 上动画图像(如电影)的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆