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

注釋很詳細....


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM