美文网首页
全局监听截屏事件

全局监听截屏事件

作者: simplez_ | 来源:发表于2018-04-25 13:50 被阅读0次

iOS7之后,苹果开放出一个通知:UIApplicationUserDidTakeScreenshotNotification,截屏时系统就会发出这个通知,需要你注册这个通知,就能捕捉到截屏图片。

//注册截屏通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(GetScreenShotAction:) name:UIApplicationUserDidTakeScreenshotNotification object:nil];

方法实现:

  • (void)GetScreenShotAction:(NSNotification *)notification{
    NSLog(@"捕捉截屏事件");

    //获取截屏图片
    UIImage *image = [UIImage imageWithData:[self imageDataScreenShot]];

    //显示图片
    UIImageView *imgV = [[UIImageView alloc]initWithImage:image];
    imgV.frame = [UIScreen mainScreen].bounds;

    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    backView.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.8];

    UIButton shareBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    shareBtn.titleLabel.font = [UIFont systemFontOfSize:17.0];
    [shareBtn setTintColor:[UIColor whiteColor]];
    shareBtn.frame = CGRectMake(SCREEN_WIDTH/5,SCREEN_HEIGHT ,SCREEN_WIDTH
    3/5,50);
    [shareBtn.layer setMasksToBounds:YES];
    [shareBtn.layer setBorderWidth:1];
    shareBtn.layer.cornerRadius = 6;
    [shareBtn setTitle:@"分享给好友" forState:UIControlStateNormal];
    shareBtn.backgroundColor = [SouFunIMUtilityHelper colorWithHexString:@"#B22222"];
    [shareBtn addTarget:self action:@selector(shareBtn:) forControlEvents:UIControlEventTouchUpInside];

    [backView addSubview:imgV];
    [backView addSubview:shareBtn];

    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:backView];

    [UIView animateWithDuration:1.0 animations:^{
    imgV.transform = CGAffineTransformMakeScale(0.8, 0.8);
    shareBtn.transform = CGAffineTransformMakeTranslation(0, -50);
    }];
    //3秒后消失
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    [backView removeFromSuperview];
    });
    }

获取截屏图片data:

  • (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);
    }

相关文章

  • Android 手势密码锁的使用细说

    监听屏幕锁屏和解锁事件 启动时进行检测 基类中全局进行监听 构造广播监听锁屏截屏事件 应用Application里...

  • 全局监听截屏事件

    iOS7之后,苹果开放出一个通知:UIApplicationUserDidTakeScreenshotNotifi...

  • ios 禁止截屏、录屏 监听事件做弹框提示

    ios不能做到禁止截屏和录屏,只能使用通知监听到截屏或录屏事件,来做一些处理,比如停止播放视频等。 如果想要监听整...

  • android home键流程解析

    上一篇文章中我们介绍了android系统的截屏事件,由于截屏事件是一种系统全局处理事件,所以事件的处理逻辑不是在A...

  • Android截屏事件监听总结

    转载注明出处:http://www.jianshu.com/p/7ead15eeaaef 前言 Android系统...

  • Android监听手机截屏事件

    1、前言 网上监听手机截屏方法教程很多了,我就记录下在学习项目中,项目用到的监听截屏方法。项目用ContentOb...

  • 系统级别截屏监听(模仿某商城)

    模仿某东商城的详情页面截屏会有弹窗事件的响应功能大体实现了,细节没做请不要纠结,进入正题。 对于系统级别截屏的监听...

  • 截屏状态监听 - iOS

    既接到电话状态监听的需求之后再次添加了截屏状态的监听,当使用 App 时若用户执行截屏操作需要对当前状态进行监听操...

  • iOS 截屏&长截屏

    截屏在 iOS 开发中经常用到,本篇文章讲的是监听用户截屏操作,并且获取截屏图片,如果当前是UIScrollVie...

  • Android 监听系统截屏操作(已适配Android Q)

    Android系统没有提供现成的Api来监听到用户的截屏操作,所以我们需要自己实现对用户截屏的监听。 针对这个特殊...

网友评论

      本文标题:全局监听截屏事件

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