ios --图片文字组合头像那点事

/**
 图片文字组合头像那点事

 @param string 昵称
 @param imageSize 图片尺寸
 @param imageColor 图片颜色
 @return 返回的 图片
 */
+ (UIImage *)gl_creatImageWithString:(NSAttributedString *)string
                           imageSize:(CGSize)imageSize
                          imageColor:(UIColor *)imageColor
{
    //通过自己创建一个context来绘制,通常用于对图片的处理
    UIGraphicsBeginImageContextWithOptions(imageSize, NO, [UIScreen mainScreen].scale);
    //获取上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    //设置填充颜色
    CGContextSetFillColorWithColor(context, imageColor.CGColor);
    //直接按rect的范围覆盖
    CGContextAddEllipseInRect(context, CGRectMake(0, 0, imageSize.width, imageSize.height));
    CGContextFillPath(context);
    
    CGSize stringSize = [string size];
    CGFloat x = (imageSize.width - stringSize.width)/2.0;
    CGFloat y = (imageSize.height - stringSize.height)/2.0;
    
    [string drawInRect:CGRectMake(x, y, stringSize.width, stringSize.height)];
    
    UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newimg;
}

猜你喜欢

转载自www.cnblogs.com/shenlaiyaoshi/p/9565364.html