iOS 获取系统截屏事件

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"测试";
    
    
    // 获取截屏事件监听
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(getScreenshot:)
                                                 name:UIApplicationUserDidTakeScreenshotNotification object:nil];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)getScreenshot:(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:self.view.bounds];
    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(120,500 ,100,50);
    [shareBtn.layer setMasksToBounds:YES];
    [shareBtn.layer setBorderWidth:1];
    shareBtn.layer.cornerRadius = 6;
    [shareBtn setTitle:@"分享给好友" forState:UIControlStateNormal];
    shareBtn.backgroundColor = [UIColor purpleColor];
    [shareBtn addTarget:self action:@selector(click) 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];
    });
}

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

复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值