• ios截屏事件监听


    目的:实现截屏反馈,类似支付宝的截屏上传反馈功能.

    1.注册全局通知,在Appdelegate中注册截屏监听通知

    - (void)registNotification{
        
         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getScreenshot:) name:UIApplicationUserDidTakeScreenshotNotification object:nil];
       
    }
    

      

    2.实现通知

    - (void)getScreenshot:(NSNotification *)notification{
        NSLog(@"捕捉截屏事件");
        UIImage *shorImg =  [UIImage imageWithData:[self imageDataScreenShot]];
    
    }
    

      

    - (NSData *)imageDataScreenShot
    {
        CGSize imageSize = CGSizeZero;
        imageSize = [UIScreen mainScreen].bounds.size;
        
        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 ([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);
    }
    

      

  • 相关阅读:
    小总结:fibonacci数的产生
    pick the stone game
    温故知新的错题训练:Coin game
    《博弈论的诡计》
    思维+博弈论:字符串操作
    一下午的编程思索录
    2018中国大学生程序设计竞赛
    温故知新的经典贪心题目:今年暑假不AC?
    2019-2020新学的一些东西(持续更新)
    【半平面交】JZOJ3297. 【SDOI2013】逃考
  • 原文地址:https://www.cnblogs.com/likun123/p/9518413.html
Copyright © 2020-2023  润新知