[Solved] Is UIGraphicsBeginImageContext thread safe?

So I’ve been under the impression that UIGraphicsBeginImageContext is thread safe, and can be called from any thread to create a new CGContextRef that can be used to draw.

However, the current documentation states that

"You should call this function from the main thread of your application only."

However, in the WWDC 2012 session video “Building Concurrent User Interfaces”, the speaker shows that you can actually draw in the background, and call that method in the background:

enter image description here

enter image description here

enter image description here

So as you can see, he adds an operation to a _queue (which runs on a BG thread), which calls renderedGraphOfSize, which calls UIGraphicsBeginImageContext. So what’s the deal? I am getting some crashes in my app when it comes to drawing in the background, so I don’t know why there are confused examples from Apple.

Enquirer: Snowman

||

Solution #1:

To quote from the documentation:

In iOS 4 and later, you may call this function from any thread of your
app.

link:http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIKitFunctionReference/Reference/reference.html

Respondent: EsbenB

Solution #2:

UIGraphicsBeginImageContext definitely is thread safe in iOS 4 and later.
WWDC 2012 session videos relate to iOS 6, so maybe you should relate to iOS 6 documentation as well 🙂

Respondent: Sascha

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.