程序员人生 网站导航

监听手机截屏事件

栏目:综合技术时间:2016-07-19 13:02:40

猴子原创,欢迎转载。转载请注明: 转载自Cocos2Der-CSDN,谢谢!
原文地址: 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 // 注意:为何不直接从相册读取截屏图象 //(万1用户直接谢绝可权限你不跪了,何况截屏以后,用户可不知道你会提示,第1反应肯定谢绝读取相册的权限) 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)! }

注意:为何不直接从相册读取截屏图象?
万1用户直接谢绝可权限你不跪了,何况截屏以后,用户可不知道你会提示,第1反应肯定谢绝读取相册的权限。

------分隔线----------------------------
------分隔线----------------------------

最新技术推荐