title: UIGraphicsBeginImageContextWithOptions and Multithreading
body: <p>I'm a bit confused about <code>UIGraphicsBeginImageContextWithOptions</code> and threading because according to <a href="http://developer.apple.com/library/ios/#documentation/uikit/reference/UIKitFunctionReference/Reference/reference.html">UIKit Function Reference</a> <code>UIGraphicsBeginImageContextWithOptions</code> 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 -<code>drawInRect:</code> for <code>UIImage</code>, <code>-drawInRect:withFont:</code> for <code>NSString</code> 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 <a href="http://developer.apple.com/library/ios/#releasenotes/General/WhatsNewIniPhoneOS/Articles/iPhoneOS4.html">What's New in iOS 4.0</a> it's written that </p>
  
  <blockquote>
    <p><strong>Drawing to a graphics context in UIKit is now thread-safe.</strong> Specifically:<br/>
    - The routines used to access and manipulate the graphics<br/>
    context can now correctly handle contexts residing on different
    threads.<br/> - String and image drawing is now thread-safe.<br/> - Using color and
    font objects in multiple threads is now safe to do.</p>
  </blockquote>
  
  <p>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 <code>UIGraphicsBeginImageContextWithOptions</code>, 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 <code>UIGraphicsBeginImageContextWithOptions</code> creates a context for the background thread and everything seems to be fine.<br/>
  So my questions are:<br/>
   - Why it's necessary to call <code>UIGraphicsBeginImageContextWithOptions</code> only on the main thread as it seems to be working fine in background?<br/>
   - How to use <code>UIImage</code>'s <code>-drawInRect:</code> 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 <code>UIGraphicsBeginImageContextWithOptions</code> there?<br/>
   - What's is the correct approach to background image manipulation using UIKit's methods (I know I can also use <code>CGBitmapContextCreate</code>, 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 <code>-drawInRect:</code> method of <code>UIImage</code>)?</p>