截圖并添加字,生成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;
    }

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容