iOS 获取APP内截屏图片并做进一步处理

项目中难免会用到屏幕的截屏,截屏之后的图片并不能满足项目的需求,这个时候你就要对获取的屏幕图片做进一步的处理,例如:打上水印或者将logo拼接到截图上然后去分享等等。所以在这里就写一下项目中使用获取到的截屏拼接上logo然后分享图片的功能实现。 Demo地址

  • 第一步:现实在APP内监听用户的截屏行为 -->在AppDelegate的方法 application:didFinishLaunchingWithOptions: 中添加通知并实现通知的方法。代码如下:
 //用户截屏操作
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(userDidTakeScreenshot:)
                                                 name:UIApplicationUserDidTakeScreenshotNotification object:nil];
//监听方法实现
- (void)userDidTakeScreenshot:(NSNotification *)notification
{
    __weak typeof(self) weakSelf = self;
    //人为截屏, 模拟用户截屏行为, 获取所截图片 避免用户连续截屏
    for (UIView *view in self.window.subviews) {
        if ([view isKindOfClass:[CustomIOSAlertView class]]) {
            return;
        }
    }
    UIImage *image = [self imageWithScreenshot];
    _popAlertView = [[CustomIOSAlertView alloc] init];
    [_popAlertView setContainerView:[self createViews:image]];
    [_popAlertView setButtonTitles:[NSMutableArray arrayWithObjects:@"保存",@"分享", nil]];
    [_popAlertView setOnButtonTouchUpInside:^(CustomIOSAlertView *alertView, int buttonIndex) {
        if (buttonIndex ==0) {
            //在保存图片的时候可以打上自定义的水印
            NSLog(@"保存");
            UIImage *saveImg = [weakSelf waterMarkForImage:image withMarkName:@"markInfo"];
            [weakSelf saveImageAlbum:saveImg];
        }
        if (buttonIndex == 1) {
            NSLog(@"分享");
        }
        [alertView close];
    }];
    [_popAlertView setUseMotionEffects:YES];
    [_popAlertView show];
}
  • 第二步要实现获取截屏并转化为图片,代码实现如下:
//获取截屏
- (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]) {

        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);
}

  • 第三步就是在获取的图片上实现打上水印 或者拼接logo或其他图片的处理。代码如下:
//给截图打上logo或者水印标志
- (UIImage *)waterMarkForImage:(UIImage *)shotImg
                  withMarkName:(NSString *)markName
{
    UIImage *codeImg = [UIImage imageNamed:@"codeImage"];
    CGFloat width = shotImg.size.width;
    CGFloat height = shotImg.size.height;
    CGFloat codeImgRatio = 220/375.0f;//拼接图片的高/宽的比例 用来适配屏幕
    UIGraphicsBeginImageContext(CGSizeMake(width, width * codeImgRatio + height));
    [shotImg drawInRect:CGRectMake(0.0, 0.0, width, height)];
    [codeImg drawInRect:CGRectMake(0.0, height, width, width * codeImgRatio)];
    //打上水印
    NSDictionary *attribute = @{
                           NSFontAttributeName: [UIFont boldSystemFontOfSize:10],  //设置字体
                           NSForegroundColorAttributeName :[UIColor orangeColor]  //设置字体颜色
                           };
    [markName drawAtPoint:CGPointMake(0, height + width * codeImgRatio - 20) withAttributes:attribute];
    //得到最终的图
    UIImage *finalImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return finalImg;
}
结尾:APP内部截图处理的部分就实现了,上面只是贴上了主要的功能代码,具体要怎么实现就要看具体的项目需求了。我遇到的只是截图之后拼接图片,然后去微信分享。这是本人的笔记,在学习的过程中记录一下,如感觉代码有不妥之处,还望指正🙏。 再次贴上 demo地址
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,117评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,328评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,839评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,007评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,384评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,629评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,880评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,593评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,313评论 1 243
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,575评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,066评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,392评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,052评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,082评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,844评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,662评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,575评论 2 270

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 170,569评论 25 707
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 11,614评论 4 59
  • 简介 elegant是Mac风格的优秀主题,简单,专注文章本身。A responsive, minimal, an...
    孤逐王阅读 3,228评论 4 3
  • 玛雅金字塔雄居奇琴伊察的正中,是为羽蛇神而建的神庙。金字塔的地基呈方形,四边依阶梯上升,直到顶端的庙宇。在春季和秋...
    落叶满阶阅读 573评论 0 0
  • 就算没有人为我鼓掌,也要一直奔跑! ①就算你觉得自己再辛苦,可孩子是你带到这个世上的,你若不坚强,让他们靠谁去?你...
    诺凡阅读 147评论 0 1