I know that it is safe to draw any thread while I call
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0); UIGraphicsEndImageContext();
in the same topic.
A screenshot using this method takes about 300 ms, which is not bad, but I'm in a tough situation, so I want to do this in the background thread.
That's what I'm doing:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0); [view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage * screenshot = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); });
The only thing in question is the view , which lies on the main thread. Is it safe to call renderInContext on view.layer from the background thread? Or, in general, is a read-only UIKit object from another thread safe?
(And please donβt give me the default βUIKit answer is not thread safe.β I already know this. This is a special case (and donβt tell me there are no special cases).)
(The code above works fine, but I'm not sure if this is just a coincidence.)
Snowman
source share