I understand that to use the UIActivityIndicator you must start it in the main thread, and then do your long running operation on a background thread. I also understand that all UIKit operations should be done on the main thread as well.
But what I am faced with 开发者_StackOverflow社区now is a long running operation to create CALayers using the Quartz CGContext drawing methods, and I'm wondering if it is possible to do the Quartz stuff in a background thread so I can display a activity indicator while my complex graphs are being drawn?
I'm pretty new to Quartz and have to admit I get lost easily in this area. I'm hoping that it is different enough from UIKit to allow background context drawing operations.
Spin off an NSThread
and draw into a context you've created via CGBitmapContextCreate
. When it's complete, use -[NSObject performSelectorOnMainThread:withObject:waitUntilDone:]
to send the image you get from CGBitmapContextCreateImage
back to the main thread to be assigned to a CALayer
's contents
property. Be sure that your drawing code is thread-safe.
You don't create CALayers using the Quartz CGContext drawing methods. You draw into CALayers using the CGContext drawing methods. This happens during the drawing phase of the run loop when it calls drawInContext:
, and you don't have control over what thread that happens on (or even when it happens really). You want to keep those as quick as possible, so for complex layers you should as much pre-calculation as you can and save off the answers, paths, etc.
I feel you may mean something else though. What do you mean by "create CALayers?"
精彩评论