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

- (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;

}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容