iOS监测用户截屏行为&自定义处理截屏图片(案例:飞猪app)

项目中需求监测用户截屏行为 并生成图片诱导用户分享。像飞猪此类APP已经实现分享功能
产品需求是改变用户系统截图的本身上的图片元素,使其截屏时保存到相册的那张图片是我们想让他保存的模样。
后来查资料发现iOS只提供了

UIApplicationUserDidTakeScreenshotNotification 这个Notification的API Key 来检测用户截屏行为 问题是Did表明是在截屏之后才告诉你已经截屏了这个状态 暂时未提供will的方法

示例截图.PNG

So 只能像飞猪一样监测到截屏之后自己处理一张截图 提供给用户是否选择分享

注册通知

 [[NSNotificationCenter defaultCenter]addObserver:self
                                            selector:@selector(userDidTakeScreenshot:)  name:UIApplicationUserDidTakeScreenshotNotification
                                              object:nil];

实现监测方法&生成UI

- (void)userDidTakeScreenshot:(NSNotification *)notification
{
    //模拟用户截屏行为, 获取所截图片(并非系统的截图 系统截图还是会保存到相册 这里可以处理我们自己的截图选择去保存或者分享)
    UIImage *image = [self imageWithScreenshot];

    UIToolbar *baseView = [[UIToolbar alloc]initWithFrame:self.view.bounds];
    baseView.barStyle = UIBarStyleBlack;
    [self.view.window addSubview:baseView];
    //添加显示
    UIImageView *imgvPhoto = [[UIImageView alloc]initWithImage:image];
    imgvPhoto.frame = CGRectMake(0, 0, self.view.frame.size.width/1.5, self.view.frame.size.height/1.5);
    imgvPhoto.center = baseView.center;
    //添加边框
    imgvPhoto.layer.borderColor = [[UIColor xingzheBlue] CGColor];
    imgvPhoto.layer.borderWidth = 5.0f;
   
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, imgvPhoto.y + imgvPhoto.height + 20, SCREENWIDTH, 40);
    [button setTitle:@"分享至好友" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor xingzheBlue] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(shareConfig) forControlEvents:UIControlEventTouchUpInside];
    [baseView addSubview:button];
    [baseView addSubview:imgvPhoto];
}

分享

- (void)shareConfig
{
    //分享代码
}

截取当前屏幕window上的元素生成Image

//截取当前屏幕
- (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);
}

//返回截取到的图片
- (UIImage *)imageWithScreenshot
{
    NSData *imageData = [self dataWithScreenshotInPNGFormat];
    return [UIImage imageWithData:imageData];
}

如果有其他更好地方法来检测用户将要截屏的行为方式欢迎交流分享....

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,254评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,196评论 4 61
  • 因病住院时,跟爱读书的浩子说让他给我推荐一些能让人从中获得温暖和力量的书籍。不仅仅是因为病情特殊导致的身心痛苦,还...
    玉妃说阅读 3,541评论 0 1
  • 不知道为什么到了这个年纪,所有人都把感情挂在嘴边,仿佛爱情就是像超市上的货品,你想要就可以得到,但是感情不应...
    side033阅读 2,444评论 0 0
  • 剃度 烂漫的青年信起了佛,他虔诚地牵着众生,他信奉万物有灵。他学习忏悔,感恩,回向,他维持善良的国度,他推崇良知和...
    南溪向南北歌流海阅读 2,312评论 0 0