• 截屏状态监听


    既接到电话状态监听的需求之后再次添加了截屏状态的监听,当使用 App 时若用户执行截屏操作需要对当前状态进行监听操作,下面有两种方法,其中可以替换截屏的图片内容(Plan A),也可以弹出提示框(Plan B),废话不多说步骤如下.

      1 #pragma mark - 监听截屏
      2 // Plan A
      3 /**
      4  监听设备截屏
      5  */
      6 - (void)registerTakeScreenShotNotice {
      7     kWeakSelf(self);
      8     NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
      9     [kNotificationCenter addObserverForName:UIApplicationUserDidTakeScreenshotNotification
     10                                      object:nil
     11                                       queue:mainQueue
     12                                  usingBlock:^(NSNotification * _Nonnull note) {
     13                                      
     14                                      NSLog(@"考试截屏");
     15                                      [weakself userDidTakeScreenshot];//屏幕响应
     16                                  }];
     17 }
     18 /**
     19  截屏响应
     20  */
     21 - (void)userDidTakeScreenshot {
     22     NSLog(@"检测到截屏");
     23     //人为操作,获取截屏图片数据
     24     UIImage *image = [self imageWithScreenshot];
     25     NSLog(@"userDidTakeScreenshot:
    %@", image);
     26     
     27     UIImageView *imageScreenshot = [[UIImageView alloc] initWithImage:image];// 此处 image 资源可根据实际需求进行操作,展示当前截屏图片或者替换成一张固定的图片方式等等等!
     28     imageScreenshot.frame = CGRectMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);
     29     [self.wkWebView addSubview:imageScreenshot];// 展示在当前 View 层级
     30 }
     31 /**
     32  返回截屏数据
     33  
     34  @return 返回截屏数据
     35  */
     36 - (UIImage *)imageWithScreenshot {
     37     NSData *imageData = [self dataWithScreenshotInPNGFormat];
     38     return [UIImage imageWithData:imageData];
     39 }
     40 /**
     41  获取当前屏幕
     42  
     43  @return 获取当前屏幕
     44  */
     45 - (NSData *)dataWithScreenshotInPNGFormat {
     46     // Source (Under MIT License):
     47     CGSize imageSize = CGSizeZero;
     48     UIInterfaceOrientation orientation = kApplication.statusBarOrientation;
     49     if (UIInterfaceOrientationIsPortrait(orientation)) {
     50         imageSize = SCREEN_RECT.size;
     51     }
     52     else {
     53         imageSize = CGSizeMake(SCREEN_HEIGHT, SCREEN_WIDTH);
     54     }
     55     
     56     UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
     57     CGContextRef context = UIGraphicsGetCurrentContext();
     58     for (UIWindow *window in [kApplication windows]) {
     59         CGContextSaveGState(context);
     60         CGContextTranslateCTM(context, window.center.x, window.center.y);
     61         CGContextConcatCTM(context, window.transform);
     62         CGContextTranslateCTM(context, -window.bounds.size.width * window.layer.anchorPoint.x, -window.bounds.size.height * window.layer.anchorPoint.y);
     63         
     64         // Correct for the screen orientation
     65         if (orientation == UIInterfaceOrientationLandscapeLeft) {
     66             CGContextRotateCTM(context, M_PI_2);
     67             CGContextTranslateCTM(context, 0, -imageSize.width);
     68         }
     69         else if (orientation == UIInterfaceOrientationLandscapeRight) {
     70             CGContextRotateCTM(context, -M_PI_2);
     71             CGContextTranslateCTM(context, -imageSize.height, 0);
     72         }
     73         else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
     74             CGContextRotateCTM(context, M_PI);
     75             CGContextTranslateCTM(context, -imageSize.width, -imageSize.height);
     76         }
     77         
     78         if ([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
     79             [window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES];
     80         }
     81         else {
     82             [window.layer renderInContext:context];
     83         }
     84         
     85         CGContextRestoreGState(context);
     86     }
     87     
     88     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
     89     UIGraphicsEndImageContext();
     90     
     91     return UIImagePNGRepresentation(image);
     92 }
     93  
     94 // Plan B
     95 - (void)intercepScreenshots {
     96 //    kWeakSelf(self);
     97 //    NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
     98 //    [kNotificationCenter addObserverForName:UIApplicationUserDidTakeScreenshotNotification object:nil queue:mainQueue usingBlock:^(NSNotification * _Nonnull note) {
     99 //        [weakself checkScreenshots];
    100 //    }];
    101     [kNotificationCenter addObserver:self
    102                             selector:@selector(checkScreenshots)
    103                                 name:UIApplicationUserDidTakeScreenshotNotification
    104                               object:nil];
    105 }
    106 - (void)checkScreenshots {
    107     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示"
    108                                                         message:@"勿截图"
    109                                                        delegate:self
    110                                               cancelButtonTitle:@"YES"
    111                                               otherButtonTitles:@"NO", nil];
    112     [alertView show];
    113 }

    此次分享到此结束,希望内容能对大家实际有所帮助,有什么不足之处欢迎指点共同进步!

  • 相关阅读:
    【WCF】无废话WCF入门教程
    【IIS8】在IIS8添加WCF服务支持
    iOS 中如何将View设置为圆角的矩形?
    在iOS开发中使用FMDB
    iOS中FMDB的使用【单例】
    普通分页笔记
    基础BaseDao
    连接池技术 实现的四个要素:jdbc.properties配置- 读取配置的单例类 --ConfigManage--BaseDao写法
    反射生成对象,调用对象方法
    context分页
  • 原文地址:https://www.cnblogs.com/survivorsfyh/p/9449412.html
Copyright © 2020-2023  润新知