Does UIView's -drawRect: have to be called on

2019-09-14 07:35发布

问题:

Does UIView's -drawRect: method have to be drawn on the main thread or can CADisplayLink call -setNeedsDisplay on a custom view in a different run loop?

回答1:

All UIKit calls should be done on the main thread.



回答2:

As of iOS 4.0, you can draw within a UIView's -drawRect: on a background thread:

  • Drawing to a graphics context in UIKit is now thread-safe. Specifically:
    • The routines used to access and manipulate the graphics context can now correctly handle contexts residing on different threads.
    • String and image drawing is now thread-safe.
    • Using color and font objects in multiple threads is now safe to do.

See also their comments in Technical Q&A QA1637 regarding this in iOS 4.0.

Any version of iOS before that still needs to have this drawing be on the main thread.