標籤:add tran and nbsp 行操作 擷取 協助 替換 code
既接到電話狀態監聽的需求之後再次添加了截屏狀態的監聽,當使用 App 時若使用者執行截屏操作需要對目前狀態進行監聽操作,下面有兩種方法,其中可以替換截屏的圖片內容(Plan A),也可以彈出提示框(Plan B),廢話不多說步驟如下.
1 #pragma mark - 監聽截屏 2 // Plan A 3 /** 4 監聽裝置截屏 5 */ 6 - (void)registerTakeScreenShotNotice { 7 kWeakSelf(self); 8 NSOperationQueue *mainQueue = [NSOperationQueue mainQueue]; 9 [kNotificationCenter addObserverForName:UIApplicationUserDidTakeScreenshotNotification 10 object:nil 11 queue:mainQueue 12 usingBlock:^(NSNotification * _Nonnull note) { 13 14 NSLog(@"考試截屏"); 15 [weakself userDidTakeScreenshot];//螢幕響應 16 }]; 17 } 18 /** 19 截屏響應 20 */ 21 - (void)userDidTakeScreenshot { 22 NSLog(@"檢測到截屏"); 23 //人為操作,擷取截屏圖片資料 24 UIImage *image = [self imageWithScreenshot]; 25 NSLog(@"userDidTakeScreenshot:\n%@", image); 26 27 UIImageView *imageScreenshot = [[UIImageView alloc] initWithImage:image];// 此處 image 資源可根據實際需求進行操作,展示當前截屏圖片或者替換成一張固定的圖片方式等等等! 28 imageScreenshot.frame = CGRectMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2); 29 [self.wkWebView addSubview:imageScreenshot];// 展示在當前 View 層級 30 } 31 /** 32 返回截屏資料 33 34 @return 返回截屏資料 35 */ 36 - (UIImage *)imageWithScreenshot { 37 NSData *imageData = [self dataWithScreenshotInPNGFormat]; 38 return [UIImage imageWithData:imageData]; 39 } 40 /** 41 擷取當前螢幕 42 43 @return 擷取當前螢幕 44 */ 45 - (NSData *)dataWithScreenshotInPNGFormat { 46 // Source (Under MIT License): 47 CGSize imageSize = CGSizeZero; 48 UIInterfaceOrientation orientation = kApplication.statusBarOrientation; 49 if (UIInterfaceOrientationIsPortrait(orientation)) { 50 imageSize = SCREEN_RECT.size; 51 } 52 else { 53 imageSize = CGSizeMake(SCREEN_HEIGHT, SCREEN_WIDTH); 54 } 55 56 UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0); 57 CGContextRef context = UIGraphicsGetCurrentContext(); 58 for (UIWindow *window in [kApplication windows]) { 59 CGContextSaveGState(context); 60 CGContextTranslateCTM(context, window.center.x, window.center.y); 61 CGContextConcatCTM(context, window.transform); 62 CGContextTranslateCTM(context, -window.bounds.size.width * window.layer.anchorPoint.x, -window.bounds.size.height * window.layer.anchorPoint.y); 63 64 // Correct for the screen orientation 65 if (orientation == UIInterfaceOrientationLandscapeLeft) { 66 CGContextRotateCTM(context, M_PI_2); 67 CGContextTranslateCTM(context, 0, -imageSize.width); 68 } 69 else if (orientation == UIInterfaceOrientationLandscapeRight) { 70 CGContextRotateCTM(context, -M_PI_2); 71 CGContextTranslateCTM(context, -imageSize.height, 0); 72 } 73 else if (orientation == UIInterfaceOrientationPortraitUpsideDown) { 74 CGContextRotateCTM(context, M_PI); 75 CGContextTranslateCTM(context, -imageSize.width, -imageSize.height); 76 } 77 78 if ([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) { 79 [window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES]; 80 } 81 else { 82 [window.layer renderInContext:context]; 83 } 84 85 CGContextRestoreGState(context); 86 } 87 88 UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 89 UIGraphicsEndImageContext(); 90 91 return UIImagePNGRepresentation(image); 92 } 93 94 // Plan B 95 - (void)intercepScreenshots { 96 // kWeakSelf(self); 97 // NSOperationQueue *mainQueue = [NSOperationQueue mainQueue]; 98 // [kNotificationCenter addObserverForName:UIApplicationUserDidTakeScreenshotNotification object:nil queue:mainQueue usingBlock:^(NSNotification * _Nonnull note) { 99 // [weakself checkScreenshots];100 // }];101 [kNotificationCenter addObserver:self102 selector:@selector(checkScreenshots)103 name:UIApplicationUserDidTakeScreenshotNotification104 object:nil];105 }106 - (void)checkScreenshots {107 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示"108 message:@"勿"109 delegate:self110 cancelButtonTitle:@"YES"111 otherButtonTitles:@"NO", nil];112 [alertView show];113 }
此次分享到此結束,希望內容能對大家實際有所協助,有什麼不足之處歡迎指點共同進步!
截屏狀態監聽 - iOS