Improve transparent image drawing performance
My Goal I'd like to improve the drawing performance of transparent images in an animation by using the fact that the background beyond the images is not changing during the animation.
The Context I need to build an animation of 100 images (~5sec). I can't use the startAnimating function of UIImageView since 100 images is too big to be put in memory. So I've decided 开发者_如何学运维to use a timer and to change directly the image in the UIImageView. The problem is that for transparent images, the drawing is too slow (5 FPS for full screen images with transparency; 22 FPS for images without transparency).
When the animation is launched, I know that the pixel beyond my images will not changed until the animation is finished.
Question Is there a way to improve the drawing performance of my transparent images by using the fact that the background beyond the image don't change during the animation ?
Note that the background beyond the image can change before I launch the animation so I can't put the background directly in my images.
Unfortunately your options will be somewhat limited. Because you do not know the background ahead of time-that is, before the animation runs-then the transparent images will need to be composited atop the background when the animation runs. I see no way to work around that.
You should be able to use the -animationImages
feature of UIImageView
, as it should not load and keep all the images in memory but rather load them when they are needed to be displayed. Have you tried this method, and if so what were the results?
The only other option I can think of would be to drop down to using OpenGL textures, possible using PVRTC compressed textures that use less memory so that you can keep more of them loaded and avoid any slowdown due to image decompression while your animation is running.
Have you tried disabling the transparency on the UIImageView and setting its background to [UIColor clearColor]? That should improve compositing performance on a UIView.
You could also look into CGLayers instead of using an UIImageView and reading images on the go in a background thread. That's probably what UIImageView does with animationImages, but you should be able to save on memory that way, given you can load images fast enough.
Are you bound by I/O or graphical performance? You could try to load 3 images in memory with NSData, and decompress / show them on screen without hitting the disk to know if that's you bottleneck. You can also try to use decompressed in-memory images to know if the decompression step is a limiting factor (you use PNGs instead of JPEGs right?).
精彩评论