监听手机截屏事件
2016-06-29 10:00
381 查看
猴子原创,欢迎转载。转载请注明: 转载自Cocos2Der-CSDN,谢谢!
原文地址: http://blog.csdn.net/cocos2der/article/details/51780954
今天无意中在百度地图中截屏路线的时候,顶部出现提示我的截屏信息。这细节挺好的,省去我后面需要使用该截屏的繁琐步骤。刚好手头空闲会,我也写个玩玩。哈哈哈~~
截屏在iOS7以前是需要使用小技巧来获取用户截屏事件的,iOS7之后,apple开放了用户截屏通知事件,所以现在做起来还是挺方便的。
注意:为什么不直接从相册读取截屏图像?
万一用户直接拒绝可权限你不跪了,何况截屏之后,用户可不知道你会提示,第一反应肯定拒绝读取相册的权限。
原文地址: http://blog.csdn.net/cocos2der/article/details/51780954
今天无意中在百度地图中截屏路线的时候,顶部出现提示我的截屏信息。这细节挺好的,省去我后面需要使用该截屏的繁琐步骤。刚好手头空闲会,我也写个玩玩。哈哈哈~~
截屏在iOS7以前是需要使用小技巧来获取用户截屏事件的,iOS7之后,apple开放了用户截屏通知事件,所以现在做起来还是挺方便的。
// This notification is posted after the user takes a screenshot (for example by pressing both the home and lock screen buttons) @available(iOS 7.0, *) public let UIApplicationUserDidTakeScreenshotNotification: String
注册通知
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.userDidTakeScreenshot), name: UIApplicationUserDidTakeScreenshotNotification, object: nil)
代码实现(swift)
/// 用户截屏完毕 func userDidTakeScreenshot() { // 当前屏幕的image // 注意:为什么不直接从相册读取截屏图像 //(万一用户直接拒绝可权限你不跪了,何况截屏之后,用户可不知道你会提示,第一反应肯定拒绝读取相册的权限) let image = imageWithScreenshot() let imageView = UIImageView(frame: CGRect(x: 50, y: 50, width: 320, height: 640)) imageView.image = image self.view.addSubview(imageView) } /// 获取当前屏幕图片 func imageWithScreenshot() -> UIImage? { let imageData = dataWithScreenshotInPNGFormat() return UIImage(data: imageData) } /// 截取当前屏幕 func dataWithScreenshotInPNGFormat() -> NSData { var imageSize = CGSizeZero let screenSize = UIScreen.mainScreen().bounds.size let orientation = UIApplication.sharedApplication().statusBarOrientation if UIInterfaceOrientationIsPortrait(orientation) { imageSize = screenSize } else { imageSize = CGSizeMake(screenSize.height, screenSize.width) } UIGraphicsBeginImageContextWithOptions(imageSize, false, 0) let context = UIGraphicsGetCurrentContext() for 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 == UIInterfaceOrientation.LandscapeLeft { CGContextRotateCTM(context, CGFloat(M_PI_2)) CGContextTranslateCTM(context, 0, -imageSize.width) } else if orientation == UIInterfaceOrientation.LandscapeRight { CGContextRotateCTM(context, -CGFloat(M_PI_2)) CGContextTranslateCTM(context, -imageSize.height, 0) } else if (orientation == UIInterfaceOrientation.PortraitUpsideDown) { CGContextRotateCTM(context, CGFloat(M_PI)) CGContextTranslateCTM(context, -imageSize.width, -imageSize.height) } if window.respondsToSelector(#selector(UIView.drawViewHierarchyInRect(_:afterScreenUpdates:))) { window.drawViewHierarchyInRect(window.bounds, afterScreenUpdates: true) } else { window.layer.renderInContext(context!) } CGContextRestoreGState(context); } let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return UIImagePNGRepresentation(image)! }
注意:为什么不直接从相册读取截屏图像?
万一用户直接拒绝可权限你不跪了,何况截屏之后,用户可不知道你会提示,第一反应肯定拒绝读取相册的权限。
相关文章推荐
- 机器学习初步练习题
- js实现仿windows文件按名称排序
- RIO-SEIO水泵流量表
- Angular新手容易碰到的坑,随时更新,欢迎订阅
- 正则表达式 ━━ 入门
- AngularJS过滤器(Filters)
- $ctags -R --fields=+iaS --extra=+q *
- UCS-2编码与UTF-8编码
- hibernate进阶之路之多对多映射(五)
- 如何获取drawable目录下的图片绝对路径
- Oracle忽略hint的几种情形
- 太用力的人跑不远
- 防止恶意频繁发送短信验证码
- Java中可变长参数的使用及注意事项
- hdu 1166
- 多源最短路(codevs 1077)
- 专题四-1006-典型Kruskal算法应用
- springmvc和spring的父子容器关系
- ubuntu 14.04使用apt-get安装最新稳定版nginx的方法
- 必做: 1041、1024、1077、2218、1183(较难)