//
// 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/, window.frame.size.height/, window.frame.size.width/, window.frame.size.height/);
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(, );
imgvPhoto.layer.shadowOpacity = 0.5;
imgvPhoto.layer.shadowRadius = 10.0;
//添加两个边阴影
imgvPhoto.layer.shadowColor = [UIColor blackColor].CGColor;
imgvPhoto.layer.shadowOffset = CGSizeMake(, );
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, );
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, , -imageSize.width);
}else if (orientation == UIInterfaceOrientationLandscapeRight)
{
CGContextRotateCTM(context, -M_PI_2);
CGContextTranslateCTM(context, -imageSize.height, );
} 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

注释很详细....

iOS检测用户截屏, 并获取所截图片的更多相关文章

  1. iOS检测用户截屏并获取所截图片

    iOS检测用户截屏并获取所截图片 微信可以检测到用户截屏行为(Home + Power),并在稍后点击附加功能按钮时询问用户是否要发送刚才截屏的图片,这个用户体验非常好.在iOS7之前, 如果用户截屏 ...

  2. Android应用内 代码截屏(获取View快照)和 禁止截屏

    1. 应用内的代码截屏(获取View的快照) Android的View类中提供了获取控件绘制缓存的方法,这种截屏的方式仅限于应用内自己的Activity界面,不需要任何权限,严格来说该方法不属于截屏, ...

  3. selenium截屏操作(也支持截长图)

    1.常用的可能是谷歌和火狐做自动化在抛异常的时候可以截屏保存 from selenium import webdriver br=webdriver.Chrome() br.maximize_wind ...

  4. [Egret]长按截屏分享、分享截屏图片、本地存储

    egret 分享有API可以把一个显示对象树渲染成一个位图纹理,我把它赋值给 HTML 的 Image 元素,就实现了图片的显示,在微信中,通过长按图片可以分享出去.当然在其他浏览器可以保存在本地. ...

  5. (腾讯视频)iOS开发之视频根据url获取第一帧图片,获取任一帧图片

    #import <AVFoundation/AVFoundation.h> + (UIImage*) thumbnailImageForVideo:(NSURL *)videoURL at ...

  6. iOS截屏并修改截图然后分享的功能实现

    一. 实现的效果类似微博的截图分享 不仅截图分享的时候还进行图片的修改,增加自己的二维码 二.实现方式 苹果在ios7之后提供了一个新的通知类型:UIApplicationUserDidTakeScr ...

  7. 截屏状态监听 - iOS

    既接到电话状态监听的需求之后再次添加了截屏状态的监听,当使用 App 时若用户执行截屏操作需要对当前状态进行监听操作,下面有两种方法,其中可以替换截屏的图片内容(Plan A),也可以弹出提示框(Pl ...

  8. iOS开发 代码 或 <Home+Power>截屏

      1. 截屏的两种简单方法, 注意这两种截图方法,都必须在视图完全加载完成后才能截图,即在 viewDidAppear 方法之后截屏,否则无法得到想要的截屏效果 (1) 利用绘图方法 renderI ...

  9. iOS - Quartz 2D 手势截屏绘制

    1.绘制手势截屏 具体实现代码见 GitHub 源码 QExtension QTouchClipView.h @interface QTouchClipView : UIView /** * 创建手势 ...

随机推荐

  1. GitChat·人工智能 | 除了深度学习,机器翻译还需要啥?

    本文开始要写作的时候,翻译圈里出了一个“爆炸性”的事件.6月27日下午,一个同传译员在朋友圈里爆料:某AI公司请这位译员去“扮演”机器同传,制造人工智能取代人工同传的“震撼”效果. 这个事件瞬间在译员 ...

  2. AspectJ注解支持

    <aop:aspectj-autoproxy/> 配置aspectj启动 AspectJAutoProxyBeanDefintionParser implements BeanDefini ...

  3. PyHook3----键盘鼠标操作

    需要安装 pywin32 安装pyHook 下载网址:https://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml 注意选择的pyHook版本一定要和python对 ...

  4. Unicode规范中的BOM 和 ISO8891-1编码

    Unicode规范中的BOM Unicode规范中有一个BOM的概念.BOM——Byte Order Mark,就是字节序标记.在这里找到一段关于BOM的说明: 在UCS 编码中有一个叫做" ...

  5. Laravel Artisan Tinker: The optimize Command

    Laravel Artisan Tinker: The optimize Command December 7, 2016 Laravel JOHN KOSTER The optimize comma ...

  6. Subarray Sorting (线段树)

    题意:给你两个长度为 n 的序列 a 和 b , 可以对 a 进行 操作: 选择一段区间[ l, r ] ,使得序列a 在这段区间里 按升序排序. 可以对a 进行任意多次操作,问 a是否有可能变成b序 ...

  7. C#如何生成setup安装文件

    当前项目状况如图: ,如何把test1打包成setup文件,用户安装后即可完成相应功能. 第一,右击“解决方案test1”->添加“新建项目”->“其他项目类型”->“安装和部署”- ...

  8. 学习andriod开发之 异步加载图片(二)--- 使用其他进度条

    大家好 我是akira上一节 我们讲到使用AsyncTask 这个类进行异步的下载 主要是涉及到一些图片的更新 这次我们继续上一个demo的改进 . 不知道你是否发现一个问题 上一节我们遗留了两个bu ...

  9. BZOJ 5330 Luogu P4607 [SDOI2018]反回文串 (莫比乌斯反演、Pollard Rho算法)

    题目链接 (BZOJ) https://www.lydsy.com/JudgeOnline/problem.php?id=5330 (Luogu) https://www.luogu.org/prob ...

  10. R-seq()

    seq(0, 1, length.out = 11) > seq(0, 1, length.out = 11) [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0 ...