I have a sequence of images that I want to animate (UIImageView
supports some basic animation but it's not sufficient for my needs).
My first approach was to use UIImageView
and set the image
property when the image. This was too slow. The reason for the poor speed was due to the drawing of the images (which surprised me; I assumed the bottle neck would be loading the image).
My second approach was to use a generic UIView
and set view.layer.contents = image.CGImage
. This gave no noticeable improvement.
Both of these approaches must be performed on the main thread. I think the poor speed is due to having to draw the image data to a CGContext
.
How can I improve the drawing speed? Is it possible draw to the context on a background thread?
What is it about your animation requirements that make using UIImageView's built-in animation service using an array of UIImages * (animationImages) and accompanying animationDuration and animationRepeatCount not work?
If you are rapidly drawing multiple images, look closely at Quartz 2D. If you are drawing , then animating (moving, zooming, etc) an image, you should be looking at Core Animation.
It sounds like Quartz 2D is what you want. Apple docs here: http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/Introduction/Introduction.html
I managed to improve performance by doing a few things:
I fixed my build process so that the PNGs were being iOS optimized. (The content for the app is being managed in a separate project which outputs a bundle. The default bundle settings are for an OS X bundle, which does not optimize PNGs).
On a background thread I:
layer.content
on the main thread to the CGImageRefUsed an
NSOperationQueue
to manage the operations.I'm sure there's a better way of doing this, but the above results in acceptable performance.