zoukankan      html  css  js  c++  java
  • iOS检测用户截屏, 并获取所截图片

    //
    //  ViewController.m
    //  CheckScreenshotDemo
    //
    //  Created by 思 彭 on 2017/4/25.
    //  Copyright © 2017年 思 彭. All rights reserved.
    
    // 检测用户截屏, 并获取所截图片
    
    #import "ViewController.h"
    
    @interface ViewController ()
    
    @property (nonatomic, strong) UIImageView *imgView;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor greenColor];
        //注册用户的截屏操作通知
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(userDidTakeScreenshot:)
                                                     name:UIApplicationUserDidTakeScreenshotNotification object:nil];
    
    }
    
    //截屏响应
    - (void)userDidTakeScreenshot:(NSNotification *)notification
    {
        NSLog(@"检测到截屏");
        
        //人为截屏, 模拟用户截屏行为, 获取所截图片
        UIImage *image_ = [self imageWithScreenshot];
        
        //添加显示
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        UIImageView *imgvPhoto = [[UIImageView alloc]initWithImage:image_];
        imgvPhoto.frame = CGRectMake(window.frame.size.width/2, window.frame.size.height/2, window.frame.size.width/2, window.frame.size.height/2);
        imgvPhoto.backgroundColor = [UIColor orangeColor];
        imgvPhoto.userInteractionEnabled = YES;
        
        //添加边框
        CALayer * layer = [imgvPhoto layer];
        layer.borderColor = [
                             [UIColor whiteColor] CGColor];
        layer.borderWidth = 5.0f;
        //添加四个边阴影
        imgvPhoto.layer.shadowColor = [UIColor blackColor].CGColor;
        imgvPhoto.layer.shadowOffset = CGSizeMake(0, 0);
        imgvPhoto.layer.shadowOpacity = 0.5;
        imgvPhoto.layer.shadowRadius = 10.0;
        //添加两个边阴影
        imgvPhoto.layer.shadowColor = [UIColor blackColor].CGColor;
        imgvPhoto.layer.shadowOffset = CGSizeMake(4, 4);
        imgvPhoto.layer.shadowOpacity = 0.5;
        imgvPhoto.layer.shadowRadius = 2.0;
        
        [[UIApplication sharedApplication].keyWindow addSubview:imgvPhoto];
        
        // 添加手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImgView:)];
        [imgvPhoto addGestureRecognizer:tap];
    }
    
    // 点击图片改变imageView位置,打印图片信息
    - (void)tapImgView: (UITapGestureRecognizer *)tap {
        
        NSLog(@"点击了图片...");
        UIImageView *imgView = (UIImageView *)tap.view;
        imgView.center = self.view.center;
        NSLog(@"点击的图片名是: %@",imgView.image);
        
        /*
         控制太打印信息:
         2017-04-25 09:28:42.272 CheckScreenshotDemo[4173:623965] 检测到截屏
         2017-04-25 09:28:44.794 CheckScreenshotDemo[4173:623965] 点击了图片...
         2017-04-25 09:28:44.795 CheckScreenshotDemo[4173:623965] 点击的图片名是: <UIImage: 0x15e3a1a0>, {640, 1136}
         */
    }
    
    /**
     *  截取当前屏幕
     *
     *  @return NSData *
     */
    - (NSData *)dataWithScreenshotInPNGFormat
    {
        CGSize imageSize = CGSizeZero;
        UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
        if (UIInterfaceOrientationIsPortrait(orientation))
            imageSize = [UIScreen mainScreen].bounds.size;
        else
            imageSize = CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);
        
        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 (orientation == UIInterfaceOrientationLandscapeLeft)
            {
                CGContextRotateCTM(context, M_PI_2);
                CGContextTranslateCTM(context, 0, -imageSize.width);
            }else if (orientation == UIInterfaceOrientationLandscapeRight)
            {
                CGContextRotateCTM(context, -M_PI_2);
                CGContextTranslateCTM(context, -imageSize.height, 0);
            } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
                CGContextRotateCTM(context, M_PI);
                CGContextTranslateCTM(context, -imageSize.width, -imageSize.height);
            }
            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);
    }
    
    /**
     *  返回截取到的图片
     *
     *  @return UIImage *
     */
    - (UIImage *)imageWithScreenshot {
        
        NSData *imageData = [self dataWithScreenshotInPNGFormat];
        return [UIImage imageWithData:imageData];
    }
    
    - (void)dealloc
    {
        [[NSNotificationCenter defaultCenter]removeObserver:self];
    }
    
    @end

    注释很详细....

  • 相关阅读:
    isEmpty和isBlank区别
    java加密算法相关
    页面跳转、替换、刷新
    打开一个网站都经过了什么
    css3动画和JS+DOM动画和JS+canvas动画比较
    canvas如何兼容IE8
    移动端的300毫秒延迟问题
    几道前端的面试题
    js执行过程
    微信查看网页源代码的方法
  • 原文地址:https://www.cnblogs.com/pengsi/p/6760375.html
Copyright © 2011-2022 走看看