Problem: I have made a class which plays a big 300 x 300 px image sequence animation of 100 frames. This class has a -start method that kicks off the animation, and then a -animate: method that walks through the frames. At every frame it fetches the big chunk of bitmap data from a png, wraps that into an UIImage and assigns that to an UIImageView's image property. Then it calls a delayed selector on itself to take the next frame:
[self performSelector:@selector(animate:) withObject:nil afterDelay:delay];
the animation itself is smooth, but the whole UI freezes up until it finished. On the iPhone Simulator the UI does not freeze. So I assume CPU is running at 100% when performing this animation.
Now the question is: What kind of multithreading-technique or strategy could help out in a situation like this?
Should I start looking at POSIX? Or any other API / Library / Framework? Or should I rely on the NSObject methods for creating threads or use NSThread for that?
I tried to put the whole animation into a new NSThread thread, but that doesn't help anything. As far as I know, I have to call any UI code on the main thread. So when updating the image property of the UIImageView during the animation, this has to be done on the main thread. So the only real part where I could "save time" for the UI-rensponsiveness is when fetching the bitmap data out of the PNG? Or is there something else that would help out?
Ola,
You mentioned putting the whole thing in a thread. That's part of the problem. The links below go into different aspects of updating the user interface from a thread.
In short, your main thread will start a child thread, the child thread will perform the extraction and other calculations, the child thread will call a function 'in the main thread' using PerformSelectorOnMainThread: (look for something like that in the pages I link to below), the function called with PerformSelectorOnMainThread will update the UI, your child thread will sleep some amount of time, your child thread will keep on tickin'.
Some useful links:
x. 9media.com/blog/?p=195
x. forums.macrumors.com/showthread.php?t=683250
x. www.xprogress.com/post-36-threading-tutorial-using-nsthread-in-iphone-sdk-objective-c
Most importantly, read the documentation for NSThread.
-isdi-
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With