Capturing full screenshot with status bar in iOS programmatically

后端 未结 7 2413
执笔经年
执笔经年 2020-12-14 03:45

I am using this code to capture a screenshot and to save it to the photo album.

-(void)TakeScreenshotAndSaveToPhotoAlbum
{
   UIWindow *window = [UIApplicati         


        
7条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-14 04:35

    Capture the full screen of iPhone, get the status bar by using KVC:

    if let snapView = window.snapshotView(afterScreenUpdates: false) {
        if let statusBarSnapView = (UIApplication.shared.value(forKey: "statusBar") as? UIView)?.snapshotView(afterScreenUpdates: false) {
            snapView.addSubview(statusBarSnapView)
        }
        UIGraphicsBeginImageContextWithOptions(snapView.bounds.size, true, 0)
        snapView.drawHierarchy(in: snapView.bounds, afterScreenUpdates: true)
        let snapImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
    }
    

提交回复
热议问题