// // ViewController.m // CheckScreenshotDemo // // Created by 思 彭 on 2017/4/25. // Copyright © 2017年 思 彭. All rights reserved. // 检测用户截屏, 并获取所截图片 #import "ViewController.h" @interface ViewController () @property (nonatomic, strong) UIImageView *imgView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor greenColor]; //注册用户的截屏操作通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userDidTakeScreenshot:) name:UIApplicationUserDidTakeScreenshotNotification object:nil]; } //截屏响应 - (void)userDidTakeScreenshot:(NSNotification *)notification { NSLog(@"检测到截屏"); //人为截屏, 模拟用户截屏行为, 获取所截图片 UIImage *image_ = [self imageWithScreenshot]; //添加显示 UIWindow *window = [UIApplication sharedApplication].keyWindow; UIImageView *imgvPhoto = [[UIImageView alloc]initWithImage:image_]; imgvPhoto.frame = CGRectMake(window.frame.size.width/2, window.frame.size.height/2, window.frame.size.width/2, window.frame.size.height/2); imgvPhoto.backgroundColor = [UIColor orangeColor]; imgvPhoto.userInteractionEnabled = YES; //添加边框 CALayer * layer = [imgvPhoto layer]; layer.borderColor = [ [UIColor whiteColor] CGColor]; layer.borderWidth = 5.0f; //添加四个边阴影 imgvPhoto.layer.shadowColor = [UIColor blackColor].CGColor; imgvPhoto.layer.shadowOffset = CGSizeMake(0, 0); imgvPhoto.layer.shadowOpacity = 0.5; imgvPhoto.layer.shadowRadius = 10.0; //添加两个边阴影 imgvPhoto.layer.shadowColor = [UIColor blackColor].CGColor; imgvPhoto.layer.shadowOffset = CGSizeMake(4, 4); imgvPhoto.layer.shadowOpacity = 0.5; imgvPhoto.layer.shadowRadius = 2.0; [[UIApplication sharedApplication].keyWindow addSubview:imgvPhoto]; // 添加手势 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImgView:)]; [imgvPhoto addGestureRecognizer:tap]; } // 点击图片改变imageView位置,打印图片信息 - (void)tapImgView: (UITapGestureRecognizer *)tap { NSLog(@"点击了图片..."); UIImageView *imgView = (UIImageView *)tap.view; imgView.center = self.view.center; NSLog(@"点击的图片名是: %@",imgView.image); /* 控制太打印信息: 2017-04-25 09:28:42.272 CheckScreenshotDemo[4173:623965] 检测到截屏 2017-04-25 09:28:44.794 CheckScreenshotDemo[4173:623965] 点击了图片... 2017-04-25 09:28:44.795 CheckScreenshotDemo[4173:623965] 点击的图片名是: <UIImage: 0x15e3a1a0>, {640, 1136} */ } /** * 截取当前屏幕 * * @return NSData * */ - (NSData *)dataWithScreenshotInPNGFormat { CGSize imageSize = CGSizeZero; UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; if (UIInterfaceOrientationIsPortrait(orientation)) imageSize = [UIScreen mainScreen].bounds.size; else imageSize = CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width); UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0); CGContextRef context = UIGraphicsGetCurrentContext(); for (UIWindow *window in [[UIApplication sharedApplication] windows]) { CGContextSaveGState(context); CGContextTranslateCTM(context, window.center.x, window.center.y); CGContextConcatCTM(context, window.transform); CGContextTranslateCTM(context, -window.bounds.size.width * window.layer.anchorPoint.x, -window.bounds.size.height * window.layer.anchorPoint.y); if (orientation == UIInterfaceOrientationLandscapeLeft) { CGContextRotateCTM(context, M_PI_2); CGContextTranslateCTM(context, 0, -imageSize.width); }else if (orientation == UIInterfaceOrientationLandscapeRight) { CGContextRotateCTM(context, -M_PI_2); CGContextTranslateCTM(context, -imageSize.height, 0); } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) { CGContextRotateCTM(context, M_PI); CGContextTranslateCTM(context, -imageSize.width, -imageSize.height); } if ([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) { [window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES]; } else { [window.layer renderInContext:context]; } CGContextRestoreGState(context); } UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return UIImagePNGRepresentation(image); } /** * 返回截取到的图片 * * @return UIImage * */ - (UIImage *)imageWithScreenshot { NSData *imageData = [self dataWithScreenshotInPNGFormat]; return [UIImage imageWithData:imageData]; } - (void)dealloc { [[NSNotificationCenter defaultCenter]removeObserver:self]; } @end
注释很详细....