123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- //
- // GetScreenImage.m
- // 截图
- //
- // Created by jcymac on 2018/4/23.
- // Copyright © 2018年 jcymac. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- #import "GetScreenImage.h"
- #import "YZMAScreenshotShareView.h"
- @interface GetScreenImage()
- @property(nonatomic,strong)NSObject *observer;
- @property(nonatomic,strong)UIView *baseView;
- @property(nonatomic,strong)YZMAScreenshotShareView *screenshotShareView;
- @end
- @implementation GetScreenImage
- static GetScreenImage *staticInstance=nil;
- #pragma mark -暴露方法
- +(instancetype)shareInstance{
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- staticInstance=[[GetScreenImage alloc]init];
-
- });
- return staticInstance;
-
- }
- -(void)startListening{
- [self stopListening];
- _observer=[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationUserDidTakeScreenshotNotification
- object:nil
- queue:nil
- usingBlock:^(NSNotification *note) {
- NSLog(@"截屏咯");
- [self userDidTakeScreenshot];
- }];
- }
- -(void)stopListening{
- if(_observer){
- [[NSNotificationCenter defaultCenter] removeObserver:_observer];
- _observer=nil;
- }
- }
- #pragma mark -内部方法
- //截屏响应
- - (void)userDidTakeScreenshot
- {
- NSLog(@"检测到截屏");
- //人为截屏, 模拟用户截屏行为, 获取所截图片
- UIImage *image = [self imageWithScreenshot];
- _screenImage=image;
- _screenshotShareView=[[YZMAScreenshotShareView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
- [self.screenshotShareView.imageView setImage:image];
- [[UIApplication sharedApplication].keyWindow addSubview:self.screenshotShareView];
-
-
- if (self.resultBlock) {
- self.resultBlock();
- }
- }
- -(UIImage *)screenImage{
- if (!_screenImage) {
- _screenImage=[self imageWithScreenshot];
- }
- return _screenImage;
- }
- - (UIImage *)imageWithScreenshot
- {
- NSData *imageData = [self dataWithScreenshotInPNGFormat];
- return [UIImage imageWithData:imageData];
- }
- //获取数据
- - (NSData *)dataWithScreenshotInPNGFormat
- {
- CGSize imageSize = CGSizeZero;
- //获取设备方向
- UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
- if (UIInterfaceOrientationIsPortrait(orientation))//如果是竖直
- imageSize = [UIScreen mainScreen].bounds.size;
- else
- //水平
- imageSize = CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);
-
- UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);//绘制绘画区域
- CGContextRef context = UIGraphicsGetCurrentContext();//获取绘图上下文
- for (UIWindow *window in [[UIApplication sharedApplication] windows])//遍历windows
- {
- //函数的作用是将当前图形状态推入堆栈
- CGContextSaveGState(context);
- //内容放到window的中心
- 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 == UIInterfaceOrientationLandscapeLeft)//home在左
- {
- CGContextRotateCTM(context, M_PI_2);
- CGContextTranslateCTM(context, 0, -imageSize.width);
- }
- else if (orientation == UIInterfaceOrientationLandscapeRight)//home在左
- {
- CGContextRotateCTM(context, -M_PI_2);
- CGContextTranslateCTM(context, -imageSize.height, 0);
- } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
- CGContextRotateCTM(context, M_PI);
- CGContextTranslateCTM(context, -imageSize.width, -imageSize.height);
- }
- if ([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])
- {
- //截图 放到内容上下文
-
- [window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES];
- }
- else
- {
- [window.layer renderInContext:context];
- }
- //更新渲染
- CGContextRestoreGState(context);
- }
-
- UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();//关闭上下文
-
- return UIImagePNGRepresentation(image);//转换成png的数据
- }
- @end
|