iOS 截屏功能

截屏-意见反馈 功能

//截屏
@property (nonatomic, strong) UIView *viewScreenShot;
@property (nonatomic, strong) UIImage *imageScreenShot;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
   // 截屏事件监听(全局)
   [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(getScreenShot:) name:UIApplicationUserDidTakeScreenshotNotification object:nil];
}

#pragma mark - 截屏

- (void)getScreenShot:(NSNotification *)notification
{
    UINavigationController *navC = (UINavigationController *)self.tabbarController.selectedViewController;
    if ([navC.visibleViewController isKindOfClass:[FeedbackViewController class]]) {
        return;
    }
    
    DLog(@"捕捉截屏事件");
    //获取截屏图片
    self.imageScreenShot = [UIImage imageWithData:[self imageDataScreenShot]];
    
    //显示图片
    self.viewScreenShot = [[UIView alloc]initWithFrame:CGRectMake(WidthScreen - 95, (HeightScreen - 125) / 2, 80, 125)];
    self.viewScreenShot.backgroundColor = [UIColor blackColor];
    
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onFeedBack)];
    [self.viewScreenShot addGestureRecognizer:tapGes];
    
    UIImageView *imageView = [[UIImageView alloc]initWithImage:self.imageScreenShot];
    imageView.frame = CGRectMake(2.5, 2.5, CGRectGetWidth(self.viewScreenShot.frame) - 5, 95);
    [self.viewScreenShot addSubview:imageView];
    
    UILabel *labelFeedBack = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(imageView.frame), CGRectGetWidth(self.viewScreenShot.frame), CGRectGetHeight(self.viewScreenShot.frame) - CGRectGetMaxY(imageView.frame))];
    labelFeedBack.text = @"意见反馈";
    labelFeedBack.textColor = HSQColorWihte;
    labelFeedBack.font = Font14;
    labelFeedBack.textAlignment = NSTextAlignmentCenter;

    [self.viewScreenShot addSubview:labelFeedBack];
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self.viewScreenShot];
    
    //5秒后消失
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.viewScreenShot removeFromSuperview];
    });
}

- (NSData *)imageDataScreenShot
{
    CGSize imageSize = CGSizeZero;
    imageSize = [UIScreen mainScreen].bounds.size;
    
    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    for (UIWindow *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 ([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);
}

- (void)onFeedBack
{
    // 处理点击事件
}

你可能感兴趣的:(iOS 截屏功能)