iOS开发-检测用户截屏, 并获取所截图片
2015-11-25 11:58
405 查看
在iOS7之前, 如果用户截屏,系统会自动取消屏幕上的所有 touch 事件,(使用 touchesCancelled:withEvent: 这个方法)那么我们就可以检测这个方法的调用,然后加载本地最新图片再加以判断来实现我们的目的。但在 iOS 7 之后,截屏不再会取消屏幕的 touch 事件,所以导致了
Snapchat 和 Facebook Poke 之类的应用在 iOS 7 刚发布时依赖于系统这个行为的功能受到影响。
如果不采取任何新措施, 我们可以让应用启动后在后台循环检测相册内最新一张照片,看它的是否符合截屏的特征。这种方法可行,但这是个笨方法,需要用户允许你的程序访问相册才可以,并且一直在后台循环会消耗更多的系统资源。
当然, 苹果封闭了一些东西, 肯定也会给你开放其他东西, 不会让你走上绝路的。
iOS7提供一个崭新的推送方法:UIApplicationUserDidTakeScreenshotNotification。只要像往常一样订阅即可知道什么时候截图了。
注意:UIApplicationUserDidTakeScreenshotNotification 将会在截图完成之后显示。现在在截图截取之前无法得到通知。
希望苹果会在iOS8当中增加
UIApplicationUserWillTakeScreenshotNotification。(只有did, 没有will显然不是苹果的风格...)
下面就写了个小demo, 检测用户截屏, 并且获取截屏照片, 显示在右下角。
(需要在真机上运行, 至少, 模拟器上我不知道如何模拟截屏行为(Home + Power), 如果你知道, 还望告知)
源码git下载链接:colin1994/TakeScreenshotTest
我这里的 userDidTakeScreenshot 总共做了3件事
1.打印检测到截屏
2.获取截屏图片。调用[self imageWithScreenshot]; 这里的imageWithScreenshot是人为截屏, 模拟用户截屏操作, 获取截屏图片。
3.显示截屏图片, 以屏幕1/4大小显示在右下角, 并且加上白色边框和阴影效果突出显示。
本文转载自hitwhylz博客,版权归hitwhylz所有
Snapchat 和 Facebook Poke 之类的应用在 iOS 7 刚发布时依赖于系统这个行为的功能受到影响。
如果不采取任何新措施, 我们可以让应用启动后在后台循环检测相册内最新一张照片,看它的是否符合截屏的特征。这种方法可行,但这是个笨方法,需要用户允许你的程序访问相册才可以,并且一直在后台循环会消耗更多的系统资源。
当然, 苹果封闭了一些东西, 肯定也会给你开放其他东西, 不会让你走上绝路的。
iOS7提供一个崭新的推送方法:UIApplicationUserDidTakeScreenshotNotification。只要像往常一样订阅即可知道什么时候截图了。
注意:UIApplicationUserDidTakeScreenshotNotification 将会在截图完成之后显示。现在在截图截取之前无法得到通知。
希望苹果会在iOS8当中增加
UIApplicationUserWillTakeScreenshotNotification。(只有did, 没有will显然不是苹果的风格...)
下面就写了个小demo, 检测用户截屏, 并且获取截屏照片, 显示在右下角。
(需要在真机上运行, 至少, 模拟器上我不知道如何模拟截屏行为(Home + Power), 如果你知道, 还望告知)
源码git下载链接:colin1994/TakeScreenshotTest
一。注册通知:
//注册通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userDidTakeScreenshot:) name:UIApplicationUserDidTakeScreenshotNotification object:nil];
二。监听截屏:
执行操作, 也就是实现上面通知对应的响应函数 -- userDidTakeScreenshot//截屏响应 - (void)userDidTakeScreenshot:(NSNotification *)notification { NSLog(@"检测到截屏"); //人为截屏, 模拟用户截屏行为, 获取所截图片 UIImage *image_ = [self imageWithScreenshot]; //添加显示 UIImageView *imgvPhoto = [[UIImageView alloc]initWithImage:image_]; imgvPhoto.frame = CGRectMake(self.window.frame.size.width/2, self.window.frame.size.height/2, self.window.frame.size.width/2, self.window.frame.size.height/2); //添加<span id="4_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="4_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=2ddc01c6d4820395&k=%B1%DF%BF%F2&k0=%B1%DF%BF%F2&kdi0=0&luki=7&mcpm=0&n=10&p=baidu&q=35027180_cpr&rb=0&rs=1&seller_id=1&sid=950382d4c601dc2d&ssp2=1&stid=0&t=tpclicked3_hc&td=2282542&tu=u2282542&u=http%3A%2F%2Fwww%2Ezgxue%2Ecom%2F158%2F1587428%2Ehtml&urlid=0" target="_blank" mpid="4" style="color: rgb(51, 51, 51); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">边框</span></a></span> 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; [self.window addSubview:imgvPhoto]; }
我这里的 userDidTakeScreenshot 总共做了3件事
1.打印检测到截屏
2.获取截屏图片。调用[self imageWithScreenshot]; 这里的imageWithScreenshot是人为截屏, 模拟用户截屏操作, 获取截屏图片。
3.显示截屏图片, 以屏幕1/4大小显示在右下角, 并且加上白色边框和阴影效果突出显示。
三。获取截屏图片
/** * 截取当前屏幕 * * @return NSData * */ - (NSData *)<span id="0_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="0_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=2ddc01c6d4820395&k=data&k0=data&kdi0=0&luki=4&mcpm=0&n=10&p=baidu&q=35027180_cpr&rb=0&rs=1&seller_id=1&sid=950382d4c601dc2d&ssp2=1&stid=0&t=tpclicked3_hc&td=2282542&tu=u2282542&u=http%3A%2F%2Fwww%2Ezgxue%2Ecom%2F158%2F1587428%2Ehtml&urlid=0" target="_blank" mpid="0" style="color: rgb(51, 51, 51); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">data</span></a></span>WithScreenshotInPNGFormat { 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 <span id="1_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="1_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=2ddc01c6d4820395&k=data&k0=data&kdi0=0&luki=4&mcpm=0&n=10&p=baidu&q=35027180_cpr&rb=0&rs=1&seller_id=1&sid=950382d4c601dc2d&ssp2=1&stid=0&t=tpclicked3_hc&td=2282542&tu=u2282542&u=http%3A%2F%2Fwww%2Ezgxue%2Ecom%2F158%2F1587428%2Ehtml&urlid=0" target="_blank" mpid="1" style="color: rgb(51, 51, 51); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">data</span></a></span>WithScreenshotInPNGFormat]; return [UIImage imageWithData:imageData]; }
本文转载自hitwhylz博客,版权归hitwhylz所有
相关文章推荐
- iOS7 毛玻璃特效代码
- iOS 7:自定义导航转场动画以及更多
- ios7 中关于导航切换影响UIScrollView的问题
- xcode5使用老控件
- ios7 留白
- 我也来说说扁平化设计
- iOS 关于UIScrollView 的contentOffset.y 下移64的问题
- [iOS]Status bar hidden
- xcode5 ios7beta编译多余log清除
- iOS7中容易被忽视的新特性
- phonegap ios7 启动页缩小,有白色底边
- IOS7状态栏标准适配
- CoreData学习记录
- iOS7新特性
- iOS7的Cell的分割线宽度不满屏,解决方法
- ios6和io7通过字符串的字数获取应该要的size
- IOS7 navgationbar遮挡下面的View的问题
- iOS(iOS7 iOS6)推送教程(一步一步实现,简单易懂)
- IOS地图与定位MapKit
- UILabel根据字符串长度获取大小,设置Frame