口袋优选

GetScreenImage.m 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // GetScreenImage.m
  3. // 截图
  4. //
  5. // Created by jcymac on 2018/4/23.
  6. // Copyright © 2018年 jcymac. All rights reserved.
  7. //
  8. #import <UIKit/UIKit.h>
  9. #import "GetScreenImage.h"
  10. #import "KBScreenshotShareView.h"
  11. @interface GetScreenImage()
  12. @property(nonatomic,strong)NSObject *observer;
  13. @property(nonatomic,strong)UIView *baseView;
  14. @property(nonatomic,strong)KBScreenshotShareView *screenshotShareView;
  15. @end
  16. @implementation GetScreenImage
  17. static GetScreenImage *staticInstance=nil;
  18. #pragma mark -暴露方法
  19. +(instancetype)shareInstance{
  20. static dispatch_once_t onceToken;
  21. dispatch_once(&onceToken, ^{
  22. staticInstance=[[GetScreenImage alloc]init];
  23. });
  24. return staticInstance;
  25. }
  26. -(void)startListening{
  27. [self stopListening];
  28. _observer=[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationUserDidTakeScreenshotNotification
  29. object:nil
  30. queue:nil
  31. usingBlock:^(NSNotification *note) {
  32. NSLog(@"截屏咯");
  33. [self userDidTakeScreenshot];
  34. }];
  35. }
  36. -(void)stopListening{
  37. if(_observer){
  38. [[NSNotificationCenter defaultCenter] removeObserver:_observer];
  39. _observer=nil;
  40. }
  41. }
  42. #pragma mark -内部方法
  43. //截屏响应
  44. - (void)userDidTakeScreenshot
  45. {
  46. NSLog(@"检测到截屏");
  47. //人为截屏, 模拟用户截屏行为, 获取所截图片
  48. UIImage *image = [self imageWithScreenshot];
  49. _screenImage=image;
  50. _screenshotShareView=[[KBScreenshotShareView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
  51. [self.screenshotShareView.imageView setImage:image];
  52. [[UIApplication sharedApplication].keyWindow addSubview:self.screenshotShareView];
  53. if (self.resultBlock) {
  54. self.resultBlock();
  55. }
  56. }
  57. -(UIImage *)screenImage{
  58. if (!_screenImage) {
  59. _screenImage=[self imageWithScreenshot];
  60. }
  61. return _screenImage;
  62. }
  63. - (UIImage *)imageWithScreenshot
  64. {
  65. NSData *imageData = [self dataWithScreenshotInPNGFormat];
  66. return [UIImage imageWithData:imageData];
  67. }
  68. //获取数据
  69. - (NSData *)dataWithScreenshotInPNGFormat
  70. {
  71. CGSize imageSize = CGSizeZero;
  72. //获取设备方向
  73. UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
  74. if (UIInterfaceOrientationIsPortrait(orientation))//如果是竖直
  75. imageSize = [UIScreen mainScreen].bounds.size;
  76. else
  77. //水平
  78. imageSize = CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);
  79. UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);//绘制绘画区域
  80. CGContextRef context = UIGraphicsGetCurrentContext();//获取绘图上下文
  81. for (UIWindow *window in [[UIApplication sharedApplication] windows])//遍历windows
  82. {
  83. //函数的作用是将当前图形状态推入堆栈
  84. CGContextSaveGState(context);
  85. //内容放到window的中心
  86. CGContextTranslateCTM(context, window.center.x, window.center.y);
  87. //仿射变换
  88. CGContextConcatCTM(context, window.transform);
  89. CGContextTranslateCTM(context, -window.bounds.size.width * window.layer.anchorPoint.x, -window.bounds.size.height * window.layer.anchorPoint.y);
  90. if (orientation == UIInterfaceOrientationLandscapeLeft)//home在左
  91. {
  92. CGContextRotateCTM(context, M_PI_2);
  93. CGContextTranslateCTM(context, 0, -imageSize.width);
  94. }
  95. else if (orientation == UIInterfaceOrientationLandscapeRight)//home在左
  96. {
  97. CGContextRotateCTM(context, -M_PI_2);
  98. CGContextTranslateCTM(context, -imageSize.height, 0);
  99. } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
  100. CGContextRotateCTM(context, M_PI);
  101. CGContextTranslateCTM(context, -imageSize.width, -imageSize.height);
  102. }
  103. if ([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])
  104. {
  105. //截图 放到内容上下文
  106. [window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES];
  107. }
  108. else
  109. {
  110. [window.layer renderInContext:context];
  111. }
  112. //更新渲染
  113. CGContextRestoreGState(context);
  114. }
  115. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  116. UIGraphicsEndImageContext();//关闭上下文
  117. return UIImagePNGRepresentation(image);//转换成png的数据
  118. }
  119. @end