美文网首页
iOS监听截屏,获取截屏图片

iOS监听截屏,获取截屏图片

作者: Code4Lift | 来源:发表于2020-04-20 13:46 被阅读0次
    - (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
    {
      //监听截屏
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(userDidTakeScreenshot:)
                                                     name:UIApplicationUserDidTakeScreenshotNotification
                                                   object:nil];
          return YES;
    }
    
    
    - (void)userDidTakeScreenshot:(NSNotification *)notification {
        NSLog(@"检测到截屏");
       [self windowsScreenShot];
    }
    + (UIImage *)windowsScreenShot{
    
        UIImage * image[2];
    
        for (int i = 0; i < 2; i++) {
    
            if (i == 0) {
    
                // 获得状态栏view的上下文以绘制图片
    
                UIView *statusBarView = nil;
    
                if (@available(iOS 13.0, *)) {
    
                    #pragma clang diagnostic push
    
                    #pragma clang diagnostic ignored "-Wundeclared-selector"
    
                    UIStatusBarManager *statusBarManager = [UIApplication sharedApplication].keyWindow.windowScene.statusBarManager;
    
                    if ([statusBarManager respondsToSelector:@selector(createLocalStatusBar)]) {
    
                        UIView *_localStatusBar = [statusBarManager performSelector:@selector(createLocalStatusBar)];
    
                        if ([_localStatusBar respondsToSelector:@selector(statusBar)]) {
    
                            statusBarView = [_localStatusBar performSelector:@selector(statusBar)];
    
                        }
    
                    }
    
                } else {
    
                    statusBarView = [[UIApplication sharedApplication] valueForKey:@"_statusBar"];
    
                    // Fallback on earlier versions
    
                }
    
                UIGraphicsBeginImageContext(statusBarView.frame.size);
    
                [statusBarView.layer renderInContext:UIGraphicsGetCurrentContext()];
    
                image[i] = UIGraphicsGetImageFromCurrentImageContext();
    
                UIGraphicsEndImageContext();
    
            } else {
    
                // 获得其他所有window,包括键盘,的上下文并绘制图片
    
                CGSize roomViewSize = [UIScreen mainScreen].bounds.size;
    
                UIGraphicsBeginImageContextWithOptions(roomViewSize, NO, 0);
    
                for (UIWindow *window in [UIApplication sharedApplication].windows) {
    
                    if (![window respondsToSelector:@selector(screen)] || window.screen == [UIScreen mainScreen]) {
    
                        [window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES];
    
                    }
    
                }
    
                image[i] = UIGraphicsGetImageFromCurrentImageContext();
    
                UIGraphicsEndImageContext();
    
            }
    
        }
    
        // 将上面得到的两张图片合并绘制为一张图片,最终得到screenshotImage
    
        UIGraphicsBeginImageContext(image[1].size);
    
        [image[1] drawInRect:CGRectMake(0, 0, image[1].size.width, image[1].size.height)];
    
        [image[0] drawInRect:CGRectMake(0, 0, image[0].size.width, image[0].size.height)];
    
        UIImage *screenshotImage = UIGraphicsGetImageFromCurrentImageContext();
    
        UIGraphicsEndImageContext();
    
        return screenshotImage;
    
    }
    

    相关文章

      网友评论

          本文标题:iOS监听截屏,获取截屏图片

          本文链接:https://www.haomeiwen.com/subject/pxorihtx.html