Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIGraphicsBeginImageContextWithOptions and Multithreading

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

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.

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)?

like image 925
graver Avatar asked Jun 07 '12 11:06

graver


1 Answers

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.

like image 165
graver Avatar answered Sep 28 '22 07:09

graver