Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7411301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:22:01+00:00 2026-05-29T06:22:01+00:00

I want to move some code that takes a couple seconds to generate a

  • 0

I want to move some code that takes a couple seconds to generate a UIImage on another thread, but im getting a context error when using

    UIGraphicsBeginImageContextWithOptions(size,false,0);

before calling the dispatch to generate the image saying “invalid context 0x0” for each operation i try to do. Is this at all possible?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:22:01+00:00Added an answer on May 29, 2026 at 6:22 am

    What’s New in iOS: iOS 4.0 says this:

    • 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.

    It sounds like you tried something like this:

    UIGraphicsBeginImageContextWithOptions(size,false,0);
    dispatch_async(someQueue, ^{
        [UIColor.whiteColor setFill];
        UIRectFill(0, 0, 20, 20);
        dispatch_async(dispatch_get_main_queue(), ^{
            UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            self.imageView.image = image;
        });
    };
    

    That won’t work because each thread has its own stack of graphics contexts (starting in iOS 4.0). You need to do it like this:

    dispatch_async(someQueue, ^{
        UIGraphicsBeginImageContextWithOptions(size,false,0);
        [UIColor.whiteColor setFill];
        UIRectFill(0, 0, 20, 20);
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        dispatch_async(dispatch_get_main_queue(), ^{
            self.imageView.image = image;
        });
    };
    

    UPDATE

    The documentation for UIGraphicsBeginImageContextWithOptions and other UIKit graphics functions now says

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

    The documentation for UIColor says

    Color objects are immutable and so it is safe to use them from multiple threads in your app.

    The documentation for UIFont says

    Font objects are immutable and so it is safe to use them from multiple threads in your app.

    However, the documentation for the UIKit NSString-drawing additions says

    The methods described in this class extension must be used from your app’s main thread.

    So you must not try something like [@"hello" drawAtPoint:CGPointZero withAttributes:attrs] from a background thread.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some existing code in C# that I'm trying to move to an
I have an application and I want to move some of the tables from
I have a repository on my PC. Now I want to move some project
I want to move a file with Ruby. How do I do that?
I have some code that downloads a file from the internet located here: http://www.amsat.org/amsat/ftp/keps/current/nasa.all
I want to create a graphical component in Delphi that is editable to some
I am attempting to write a two-pass algorithm incorporating some legacy code. I want
I've got some code that needs to scan through a hierarchical database schema, looking
I have some code that has got me very close to what Im trying
I have a multi-thread C# application that uses some recursive functions in a dll.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.