ios--检测用户截屏, 并获取所截图片

来源:互联网 发布:机房网络布线方案 编辑:程序博客网 时间:2024/05/29 03:57

原文转自:http://blog.csdn.net/hitwhylz/article/details/38386979


微信可以检测到用户截屏行为(Home + Power),并在稍后点击附加功能按钮时询问用户是否要发送刚才截屏的图片,这个用户体验非常好。于是乎, 我也想着实现这个功能。


在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


一。注册通知:

[objc] view plaincopy
  1. //注册通知  
  2. [[NSNotificationCenter defaultCenter] addObserver:self  
  3.                                          selector:@selector(userDidTakeScreenshot:)  
  4.                                              name:UIApplicationUserDidTakeScreenshotNotification object:nil];  


二。监听截屏:

执行操作, 也就是实现上面通知对应的响应函数  -- userDidTakeScreenshot

[objc] view plaincopy
  1. //截屏响应  
  2. - (void)userDidTakeScreenshot:(NSNotification *)notification  
  3. {  
  4.     NSLog(@"检测到截屏");  
  5.       
  6.     //人为截屏, 模拟用户截屏行为, 获取所截图片  
  7.     UIImage *image_ = [self imageWithScreenshot];  
  8.       
  9.     //添加显示  
  10.     UIImageView *imgvPhoto = [[UIImageView alloc]initWithImage:image_];  
  11.     imgvPhoto.frame = CGRectMake(self.window.frame.size.width/2self.window.frame.size.height/2self.window.frame.size.width/2self.window.frame.size.height/2);  
  12.       
  13.     //添加边框  
  14.     CALayer * layer = [imgvPhoto layer];  
  15.     layer.borderColor = [  
  16.                          [UIColor whiteColor] CGColor];  
  17.     layer.borderWidth = 5.0f;  
  18.     //添加四个边阴影  
  19.     imgvPhoto.layer.shadowColor = [UIColor blackColor].CGColor;  
  20.     imgvPhoto.layer.shadowOffset = CGSizeMake(00);  
  21.     imgvPhoto.layer.shadowOpacity = 0.5;  
  22.     imgvPhoto.layer.shadowRadius = 10.0;  
  23.     //添加两个边阴影  
  24.     imgvPhoto.layer.shadowColor = [UIColor blackColor].CGColor;  
  25.     imgvPhoto.layer.shadowOffset = CGSizeMake(44);  
  26.     imgvPhoto.layer.shadowOpacity = 0.5;  
  27.     imgvPhoto.layer.shadowRadius = 2.0;  
  28.   
  29.     [self.window addSubview:imgvPhoto];  
  30. }  


我这里的 userDidTakeScreenshot 总共做了3件事

1.打印检测到截屏

2.获取截屏图片。调用[self imageWithScreenshot]; 这里的imageWithScreenshot是人为截屏, 模拟用户截屏操作, 获取截屏图片。

3.显示截屏图片, 以屏幕1/4大小显示在右下角, 并且加上白色边框和阴影效果突出显示。


三。获取截屏图片

[objc] view plaincopy
  1. /** 
  2.  *  截取当前屏幕 
  3.  * 
  4.  *  @return NSData * 
  5.  */  
  6. - (NSData *)dataWithScreenshotInPNGFormat  
  7. {  
  8.     CGSize imageSize = CGSizeZero;  
  9.     UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;  
  10.     if (UIInterfaceOrientationIsPortrait(orientation))  
  11.         imageSize = [UIScreen mainScreen].bounds.size;  
  12.     else  
  13.         imageSize = CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);  
  14.       
  15.     UIGraphicsBeginImageContextWithOptions(imageSize, NO0);  
  16.     CGContextRef context = UIGraphicsGetCurrentContext();  
  17.     for (UIWindow *window in [[UIApplication sharedApplication] windows])  
  18.     {  
  19.         CGContextSaveGState(context);  
  20.         CGContextTranslateCTM(context, window.center.x, window.center.y);  
  21.         CGContextConcatCTM(context, window.transform);  
  22.         CGContextTranslateCTM(context, -window.bounds.size.width * window.layer.anchorPoint.x, -window.bounds.size.height * window.layer.anchorPoint.y);  
  23.         if (orientation == UIInterfaceOrientationLandscapeLeft)  
  24.         {  
  25.             CGContextRotateCTM(context, M_PI_2);  
  26.             CGContextTranslateCTM(context, 0, -imageSize.width);  
  27.         }  
  28.         else if (orientation == UIInterfaceOrientationLandscapeRight)  
  29.         {  
  30.             CGContextRotateCTM(context, -M_PI_2);  
  31.             CGContextTranslateCTM(context, -imageSize.height0);  
  32.         } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {  
  33.             CGContextRotateCTM(context, M_PI);  
  34.             CGContextTranslateCTM(context, -imageSize.width, -imageSize.height);  
  35.         }  
  36.         if ([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])  
  37.         {  
  38.             [window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES];  
  39.         }  
  40.         else  
  41.         {  
  42.             [window.layer renderInContext:context];  
  43.         }  
  44.         CGContextRestoreGState(context);  
  45.     }  
  46.       
  47.     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();  
  48.     UIGraphicsEndImageContext();  
  49.       
  50.     return UIImagePNGRepresentation(image);  
  51. }  
  52.   
  53. /** 
  54.  *  返回截取到的图片 
  55.  * 
  56.  *  @return UIImage * 
  57.  */  
  58. - (UIImage *)imageWithScreenshot  
  59. {  
  60.     NSData *imageData = [self dataWithScreenshotInPNGFormat];  
  61.     return [UIImage imageWithData:imageData];  
  62. }  

0 0