全局監(jiān)聽截屏事件

iOS7之后,蘋果開放出一個通知:UIApplicationUserDidTakeScreenshotNotification,截屏?xí)r系統(tǒng)就會發(fā)出這個通知,需要你注冊這個通知,就能捕捉到截屏圖片。

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

方法實現(xiàn):

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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容