-1

I wonder how can I take a screenshot in a global queue? right now I'm doing it in the main queue and it works, if I do it in the global queue, things freeze. I'm using this screenshot code: iOS: what's the fastest, most performant way to make a screenshot programmatically? I also tried the following code to take the snapshot of self.view, but it also doesn't work:

+(UIImage *)snapshot_of_view:(UIView*)view {
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [UIScreen mainScreen].scale);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

any ideas?

Community
  • 1
  • 1
ios learner
  • 197
  • 1
  • 2
  • 10

2 Answers2

1

Any UI operation MUST be performed in the main thread.

Matteo Gobbi
  • 17,697
  • 3
  • 27
  • 41
  • I saw this post (http://stackoverflow.com/questions/11528803/is-uigraphicsbeginimagecontext-thread-safe) say that UIGraphicsBeginImageContext is thread safe, does that mean I can do UI..context on any thread? – ios learner Apr 22 '15 at 19:54
0

You are calling user interface code. UI code must be on the main thread, since that thread has the main run loop and stuff.

You need to do it on the main thread:

+(UIImage *)snapshot_of_view:(UIView*)view {
    dispatch_async(dispatch_get_main_queue(), ^{
        UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [UIScreen mainScreen].scale);
        [view.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    });
}
NobodyNada
  • 7,529
  • 6
  • 44
  • 51