3

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.

CedricSoubrie
  • 6,657
  • 2
  • 39
  • 44

2 Answers2

2

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.

Jason Foreman
  • 2,146
  • 18
  • 15
  • I've tried animationImages but I have way too much images for it to work (around 100 images). I think it's loading all images when you launch startAnimating Do you have a tuto or something for the PVRTC thing ? – CedricSoubrie Aug 12 '11 at 12:23
0

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?).

ndfred
  • 3,842
  • 2
  • 23
  • 14
  • Good idea but making the image Opaque don't solve the problem. Can you tell me more info of what I could do with layers to draw my image faster than what UIImageView is doing ? – CedricSoubrie Aug 12 '11 at 17:22
  • Layers are basically Open GL textures that are handled natively by [Core Animation](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreAnimation_guide/Introduction/Introduction.html): it's the fastest way to get something on the screen. If you want to save memory, focus on getting from file to layer quickly enough to feed your animation. – ndfred Aug 12 '11 at 18:51
  • Thanks. I'll try your suggestion tomorrow. As my bounty will be over before that, I give you the reward without testing your solution. I hope you'll be right ;-) – CedricSoubrie Aug 15 '11 at 23:17
  • Thanks :) The asynchronous loader code won't be easy to write, but given the iPad/iPhone's graphical power, I guess I/O will be your the bottleneck and that's what you'll have to concentrate on. – ndfred Aug 17 '11 at 00:27
  • Ok, I've tested to load everything in NSData but it doesn't solve the problem. It seems, the bottle neck is the PNG decompression. I used this : http://stackoverflow.com/questions/1815476/cgimage-uiimage-lazily-loading-on-ui-thread-causes-stutter which make my code go really fast but consume a lot of memory. Now I need to load/unload images in an intelligent manner. – CedricSoubrie Aug 22 '11 at 17:30