截图并添加字,生成image

  • (UIImage *)cropImageWithValue:(CGFloat)value {
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, value);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    // 加字
    [image drawAtPoint : CGPointMake ( 0 , 0 )];

    // 获得一个位图图形上下文
    CGContextRef imageContext= UIGraphicsGetCurrentContext();

    CGContextDrawPath (imageContext, kCGPathStroke );
    NSString str;
    if (self.isGroup) {
    str = @"长按识别/扫描二维码 加入群聊";
    }else {
    str = @"长按识别/扫描二维码 加我为好友";
    }
    CGSize textSize = [str boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil].size;
    [str drawAtPoint : CGPointMake((self.view.width-textSize.width)
    0.5, image.size.height-25) withAttributes : @{ NSFontAttributeName:[UIFont systemFontOfSize:15], NSForegroundColorAttributeName:[UIColor blackColor]}];

    // 返回绘制的新图形
    UIImage *textImage= UIGraphicsGetImageFromCurrentImageContext ();
    UIGraphicsEndImageContext ();

    return textImage;
    }

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

推荐阅读更多精彩内容