多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區(qū)平臺 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當前位置:首頁 > php開源 > 綜合技術 > 監(jiān)聽手機截屏事件

監(jiān)聽手機截屏事件

來源:程序員人生   發(fā)布時間:2016-07-19 13:02:40 閱讀次數(shù):3352次

猴子原創(chuàng),歡迎轉載。轉載請注明: 轉載自Cocos2Der-CSDN,謝謝!
原文地址: http://blog.csdn.net/cocos2der/article/details/51780954

今天無意中在百度地圖中截屏線路的時候,頂部出現(xiàn)提示我的截屏信息。這細節(jié)挺好的,省去我后面需要使用該截屏的繁瑣步驟。恰好手頭空閑會,我也寫個玩玩。哈哈哈~~

截屏在iOS7之前是需要使用小技能來獲得用戶截屏事件的,iOS7以后,apple開放了用戶截屏通知事件,所以現(xiàn)在做起來還是挺方便的。

// 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)

代碼實現(xiàn)(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反應肯定謝絕讀取相冊的權限。

生活不易,碼農辛苦
如果您覺得本網(wǎng)站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 亚洲 春色 另类小说 | 欧美日本在线播放 | 国产成人精品日本亚洲网站 | 免费黄色的网站 | 成年人在线视频免费观看 | 欧美啊v在线 | 手机在线看片国产日韩生活片 | 亚洲 欧美 精品 | 国产一级aaa全黄毛片 | 国产亚洲精品久久精品6 | 激情在线观看视频 | xxxxx做受大片视频免费 | 天天欧美 | 密桃av| 福利视频一二三在线视频免费观看 | 国产成人综合久久精品亚洲 | 色婷婷影院在线视频免费播放 | 亚洲偷怕| 日本道色综合久久影院 | 国产精品久久久久久久成人午夜 | www.黄色一片| 亚洲码欧美码一区二区三区 | 欧美疯狂| 国产精品成人扳一级aa毛片 | 网站国产| 乱人伦99久久 | 亚洲情人网 | 最近高清中文在线观看国语字幕7 | 中文字幕亚洲综合久久2 | 在线视频一区二区三区四区 | 国产成人不卡 | 久久久久777777人人人视频 | 亚洲福利视频一区 | 亚洲视频在线观看视频 | free性欧美xxx | 欧美疯狂| 思思久久这里只精品99re66 | 亚洲人成伊人成综合网久久久 | 一级做a爱| 成人精品国产 | 国产精品99久久久久久夜夜嗨 |