美文网首页
iOS 监听系统截屏,快捷问题反馈 + 模拟系统截屏生成图片

iOS 监听系统截屏,快捷问题反馈 + 模拟系统截屏生成图片

作者: Yimmm | 来源:发表于2025-02-06 10:54 被阅读0次

开发快捷问题反馈入口,需要监听系统级的截屏事件,然后获取截屏图片快速跳转问题反馈入口。

因为iOS现在新的截屏事件并不会立即把图片存入相册,所以无法第一时间去相册获取图片,需要自己手动模拟截屏的动作,生成一张对应的图片。

这里写的是OC的版本,因为旧的项目用的是OC写的,这边事件写在私有库里,需要提供给OC和Swift的项目使用。

一、设计图(实际运行效果)

IMG_8752.PNG

如图所示,在监听系统截图的同时,生成对应的图片,弹出对应的问题反馈入口,延迟3秒后消失。

二、手动生成截屏图片

生成截屏图片的方法放在基础拓展库作为通用:


// 获取截屏UIImage
+ (UIImage *)imageWithScreenshot
{
    NSData *imageData = [self dataWithScreenshotInPNGFormat];
    return [UIImage imageWithData:imageData];
}

// 获取截屏NSData
+ (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])
    {
        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 (orientation == UIInterfaceOrientationLandscapeLeft)
        {
            CGContextRotateCTM(context, M_PI_2);
            CGContextTranslateCTM(context, 0, -imageSize.width);
        }
        else if (orientation == UIInterfaceOrientationLandscapeRight)
        {
            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);
}

三、监听系统截屏,生成问题反馈快捷入口

viewDidLoad 中,添加监听截图通知

    // 监听截图动作
    [[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(userDidTakeScreenshot:)
    name:UIApplicationUserDidTakeScreenshotNotification object:nil];

添加 @selector(userDidTakeScreenshot:) 事件,触发截屏+快捷问题反馈

#pragma mark - 监听屏幕截图,快捷问题反馈
- (void)userDidTakeScreenshot:(NSNotification *)notification
{
    NSLog(@"检测到截屏");
    // 人为截屏, 模拟用户截屏行为, 获取所截图片
    UIImage *image_ = [UIImage imageWithScreenshot];
    // 容器
    UIView *contentView = [[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width - 64 - 15, (self.view.frame.size.height - 158)/2.0, 64, 158)];
    [contentView setUserInteractionEnabled:YES];
    
    // 添加显示
    UIImageView *imgvPhoto = [[UIImageView alloc]initWithImage:image_];
    imgvPhoto.frame = CGRectMake(0, 0, 64, 125);
    imgvPhoto.layer.cornerRadius = 4.0f;
    imgvPhoto.layer.masksToBounds = YES;
    
    CALayer * layer = [imgvPhoto layer];
    layer.borderColor = [[UIColor whiteColor] CGColor];
    layer.borderWidth = 4.0f;
    layer.cornerRadius = 4.0f;
    //添加四个边阴影
    imgvPhoto.layer.shadowColor = [UIColor blackColor].CGColor;
    imgvPhoto.layer.shadowOffset = CGSizeMake(0, 2);
    imgvPhoto.layer.shadowOpacity = 1;
    imgvPhoto.layer.shadowRadius = 4;
    [contentView addSubview:imgvPhoto];
    
    
    // 添加反馈按钮
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 130, 64, 28)];
    button.backgroundColor = [UIColor hexStringToColor:@"#000000" alpha:0.5];
    [button cornerRadisus:4];
    [button setTitle:@"问题反馈" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:12];
    [button setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    [button addTarget:self action:@selector(questionShare) forControlEvents:UIControlEventTouchUpInside];
    [button setUserInteractionEnabled:YES];
    [contentView addSubview:button];
    
    [self.view addSubview:contentView];
    
    // 5秒后移除快捷问题反馈
    WEAKSELF
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:0.5 animations:^{
            STRONGSELF
            // 渐渐移出屏幕
            [contentView setX:strongSelf.view.frame.size.width + 15.0];
        } completion:^(BOOL finished) {
            STRONGSELF
            if (finished) {
                [contentView removeFromSuperview];
            }
        }];
    });
}

/// 截图问题反馈
- (void)questionShare
{

}

最后,就简单的完成了快捷问题反馈的功能开发!可喜可贺~

最最最后,完结撒花

告辞.jpeg

相关文章

  • iOS开发——监听系统截屏并获得截图

    iOS开发——监听系统截屏并获得截图 项目需要得到获得截屏然后生成自己的分享图,某度能找到的基本都是怎么获取Vie...

  • iOS 监听用户截屏并获取

    集团考勤最新的意见反馈需求,参照了京东的截屏反馈。重点就是如何监听到 用户触发了系统级的截屏,并获取到当前截屏图片...

  • 关于iOS上线应用的截图问题

    模拟器截屏统一在command + 1的情况下(实际屏幕大小),否则提示图片错误 模拟器截屏快捷键command ...

  • iOS 屏幕录制监听并获取截取的图片

    1.屏幕截取监听,iOS会监听系统截屏操作,加上通知即可 2.在userDidTakeScreenshot进行操作...

  • android 截屏实现

    Android 截屏分为四种:View 截屏、WebView 截屏、系统截屏 和 adb 截屏 1、View 截屏...

  • iOS 应用内截屏分享

    需求:捕获用户截屏操作,并建议用户截屏后的操作。虽然iOS11 有系统的截屏,但 APP 内截屏可便捷操作。 封装...

  • Quartz 2D (2)

    1、圆形图片裁剪 2、实现手机屏幕截屏功能(把控制器中View的内容截屏生成一张新的图片) 3、图片截屏 4、图片...

  • iOS 截屏&长截屏

    截屏在 iOS 开发中经常用到,本篇文章讲的是监听用户截屏操作,并且获取截屏图片,如果当前是UIScrollVie...

  • 读袁靖老师关于长城保护的政协提案有感(修订版)

    截屏图片一: 截屏图片二: 截屏图片三: 截屏图片四: 正文: 当看到袁靖老师这一政协提案时,第一印象,这...

  • (最新)iOS截屏

    ios webview 截屏:ios截屏 前言:介绍一下截屏有很多种做法1:截当前屏幕内容2:截整个视图的所有内容...

网友评论

      本文标题:iOS 监听系统截屏,快捷问题反馈 + 模拟系统截屏生成图片

      本文链接:https://www.haomeiwen.com/subject/nppnujtx.html