简体   繁体   中英

why using NSOperation, GCD or performSelectorInBackground I can't take snapshot of UIView?

I'm trying to take screenshotot of my UIView with method below. It works only if I don't try to do this in "background". But i need it in background, because each time it fires, I got horrible ui responsibility - nothing works for a few seconds.

This method works, but it stucks forever, with mach_msg_trap message when I pause it.

-(void)takeShoot{

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{

        float height;
        UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
        if(orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight){
            height = 600;
        }else{
            height = 500;
        }
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(self.view.frame.size.width, height), YES, 0.6);

        [self.view drawViewHierarchyInRect:CGRectMake(0, -60, self.view.frame.size.width, self.view.frame.size.height) afterScreenUpdates:YES];

                self.sharedImage = UIGraphicsGetImageFromCurrentImageContext();

            UIGraphicsEndImageContext();



    });
}

When I was fire it in NSOperation it get even worse -it just won't do this without any reason. And third method performSelectorInBackground is just rubbish, if I fire it method each time when uiwebview finish loading, then there will bee billion threads and thats I think isn't good at all.

So, how can I take screenshoot in background(not on mainThread ), not killing or freezing anyone?

You must take screenshots on the main thread. This is the only thread where the UI is stable.

If you have problems for several seconds, I suspect you have some other problem going on that this is just exposing. The most likely is other UIKit calls you're making on background threads. That's not supported and would lead to the symptom you describe.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM