移动开发iOS开发你需要知道的iOS备忘录

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

2017-09-30  本文已影响87人  iii余光

项目中需求监测用户截屏行为 并生成图片诱导用户分享。像飞猪此类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];
}

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

上一篇 下一篇

猜你喜欢

热点阅读