UIGraphicsBeginImageContextWithOptions和Multithreading [英] UIGraphicsBeginImageContextWithOptions and Multithreading
问题描述
我对 UIGraphicsBeginImageContextWithOptions
和线程感到有点困惑,因为根据UIKit函数参考 UIGraphicsBeginImageContextWithOptions
应仅在主线程上调用。当调用它时,它会创建一个基于位图的上下文,可以使用CoreGraphics的函数或使用 - drawInRect:
等方法来操作 UIImage
, -drawInRect:withFont:
for NSString
等等。对于CoreGraphics的绘图,一切都很清楚 - 您传递了一个CGContextRef参数,该参数被操作到每个函数,但UIKit绘图方法使用堆栈中的当前上下文。在 iOS 4.0中的新功能的发行说明中,写了
I'm a bit confused about UIGraphicsBeginImageContextWithOptions
and threading because according to UIKit Function Reference UIGraphicsBeginImageContextWithOptions
should be called only on the main thread. When called it creates a bitmap-based context, which is ready to be manipulated either with CoreGraphics' functions or with methods like -drawInRect:
for UIImage
, -drawInRect:withFont:
for NSString
and so on. For CoreGraphics' drawing everything is clear - you pass a CGContextRef argument that is being manipulated to every function, but the UIKit drawing methods use the current context in the stack. In the release notes of What's New in iOS 4.0 it's written that
在UIKit中绘制图形上下文现在是线程安全的。具体来说:
- 用于访问和操作图形的例程
上下文现在可以正确处理驻留在不同
线程上的上下文。
- 字符串和图像绘制现在是线程安全的。
- 现在可以安全地使用多个线程中的颜色和
字体对象。
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.
到目前为止一切顺利。有趣的是我有一个项目,在那里我做一些密集的绘图并通过使用 UIGraphicsBeginImageContextWithOptions
创建上下文来创建多个图像,但是当这些操作碰巧更耗时时我只是在后台线程中移动绘图,当准备好在屏幕上显示一些动画时,一切正常 - 没有崩溃,没有泄漏。图像按预期绘制,似乎 UIGraphicsBeginImageContextWithOptions
为后台线程创建一个上下文,一切似乎都没问题。
所以我的问题是:
- 为什么只需要在主线程上调用 UIGraphicsBeginImageContextWithOptions
,因为它似乎在后台正常工作?
- 如何使用 UIImage
的 -drawInRect:
方法,例如,在后台线程中,我不知道没有当前的上下文,我似乎无法创建一个,因为我无法调用 UIGraphicsBeginImageContextWithOptions
那里?
- 什么是使用UIKit的方法正确处理背景图像处理(我知道我也可以使用 CGBitmapContextCreate
,但是它既不会将创建的上下文推送到上下文堆栈中,也不是我能够做到的我自己为了使用 -drawInRect:
UIImage
的方法?)
So far so good. The interesting part is that I have a project, where I do some intensive drawings and create multiple images by creating context with UIGraphicsBeginImageContextWithOptions
, but when these operations happened to be more time-consuming and I just moved the drawing in a background thread and when ready display them on the screen with some animations and everything works just fine - no crashes, no leaks. Images draw as they are expected and it seems that UIGraphicsBeginImageContextWithOptions
creates a context for the background thread and everything seems to be fine.
So my questions are:
- Why it's necessary to call UIGraphicsBeginImageContextWithOptions
only on the main thread as it seems to be working fine in background?
- How to use UIImage
's -drawInRect:
method for example, in a background thread, where I don't have a current context and I can't seem be able create one because I can't call UIGraphicsBeginImageContextWithOptions
there?
- What's is the correct approach to background image manipulation using UIKit's methods (I know I can also use CGBitmapContextCreate
, but it neither pushes the created context into the context stack nor I seem to be able to do it myself in order to use -drawInRect:
method of UIImage
)?
推荐答案
因此,经过几天的调查,如何操作UIKit上下文是线程安全的,但你似乎无法创建一个在主要线程以外的线程中,因为 UIGraphicsBeginImageContextWithOptions
应仅在主线程上调用,但仍然这样做完全正常并且在阅读了关于主题的一些小帖子之后在Apple的开发者论坛上与其他人讨论它我可以清楚地说明,文档中有关 UIGraphicsBeginImageContextWithOptions
, UIGraphicsPushContext
和 UIGraphicsPopContext
是错误,可以调用这些方法,并且在另一个线程中使用上下文没有问题。所以 UIGraphicsBeginImageContextWithOptions
, UIGraphicsPushContext
和 UIGraphicsPopContext
是主题-safe 。
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.
这篇关于UIGraphicsBeginImageContextWithOptions和Multithreading的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!