Capture iPhone screen with status bar included?

后端 未结 4 1460
星月不相逢
星月不相逢 2020-12-10 07:36

I am looking for a way to capture a screenshot on the iPhone with the top status bar included, I am currently using the following code:

    UIGraphicsBeginIm         


        
4条回答
  •  轮回少年
    2020-12-10 08:00

    Instead of using private API, why not render the entire UIWindow into the image context? It might be enough to replace self.view with self.view.window in your code.

    You can also get the current window(s) as a property of the [UIApplication sharedApplication] instance. It's possible the status bar is on a separate window layer and maybe you'll have to render the windows in order.

    Something like this:

    UIGraphicsBeginImageContext(self.view.window.frame.size);
    for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
       [window.layer renderInContext:UIGraphicsGetCurrentContext()];
    }
    UIImage *screenshotImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
    

    At any rate, you probably don't need to resort to private API.

提交回复
热议问题