UIGraphicsBeginImageContextWithOptions and Multithreading

纵然是瞬间 提交于 2019-12-02 21:04:51

So, after a couple of days of investigating, how come manipulating the UIKit context is thread-safe and yet you don't seem to be able to create one in a thread other than the main because UIGraphicsBeginImageContextWithOptions "should be called only on the main thread", but still doing so works perfectly fine and after reading some small posts on the subject and discussing it with other guys on Apple's developer forums I can clearly state that, what's said in the documentation about UIGraphicsBeginImageContextWithOptions, UIGraphicsPushContext and UIGraphicsPopContext is WRONG and those methods could be called and the context used in another thread without problems. So UIGraphicsBeginImageContextWithOptions, UIGraphicsPushContext and UIGraphicsPopContext are thread-safe.

Partial answer: Use UIGraphicsPushContext to push your own CG context to the top of the "UIKit context" stack and everything (from that thread, as I interpret Apple's documentation) will paint to that. Don't forget to use UIGraphicsPopContext to pop it from the UIKit context stack.

This applies to all UIKit drawing operations; for example drawing UIImages, like you asked. Push a context, draw, then pop the context.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!