美文网首页
Mac默认头像

Mac默认头像

作者: 大漠赏花 | 来源:发表于2018-02-07 16:14 被阅读0次

// 用户Account的hashCode取绝对值和颜色列表总数取余,即背景色对应的位置。图片上的文字为用户name首字(英文则转为大写)

+ (NSImage*)imageFrame:(CGRect)rect withStr:(NSString*)text name:(NSString*)name

{

    if (text.length == 0) return [NSImage imageNamed:@"ic_head_default_img.png"]; // 账号为空,返回默认头像图标

    if (name.length == 0) return [NSImage imageNamed:@"ic_head_default_img.png"]; // 名字为空,返回默认头像图标

    NSArray*colors = [selfcolorLists];// 获取颜色列表

    inthashCode = [selfgetHashCode:text];// 仿java hashCode

    inthashAbs =abs(hashCode);

    intnum = hashAbs % colors.count;

    NSString*theColor = colors[num];

    NSColor*color = [NSColorcolorWithHexString:theColor];

    NSString *str = [name substringToIndex:1];

    str = [strcapitalizedStringWithLocale:[NSLocale currentLocale]]; // 转为大写字母

    NSImage*image = [selfcreateImage:rectwithColor:colorstr:str];

    returnimage;

}

// 仿java hashCode

+ (int)getHashCode:(NSString*)str {

    inthash =0;

    for(inti =0; i<[strlength]; i++) {

        NSString *s = [str substringWithRange:NSMakeRange(i, 1)];

        char *unicode = (char *)[s cStringUsingEncoding:NSUnicodeStringEncoding];

        intcharactorUnicode =0;

        size_tlength =strlen(unicode);

        for(intn =0; n < length; n ++) {

            charactorUnicode += (int)((unicode[n] &0xff) << (n *sizeof(char) *8));

        }

        hash = hash *31+ charactorUnicode;

    }

    returnhash;

}

// 颜色列表

+ (NSArray*)colorLists

{

    return @[@"0x704d4e",

             @"0x973444",

             @"0x546b83",

             @"0x2578b5",

             @"0x5c8987",

             @"0xb49436",

             @"0xfcb1aa",

             @"0xe57373",

             @"0xf06292",

             @"0x9575cd",

             @"0x7986cb",

             @"0x64b5f6",

             @"0x4fc3f7",

             @"0x4dd0e1",

             @"0x4db6ac",

             @"0x81c784",

             @"0xaed581",

             @"0xff8a65",

             @"0xffd54f",

             @"0xffb74d",

             @"0xa1887f",

             @"0x90a4ae"];

}

// 生成图片

+ (NSImage*)createImage:(CGRect)rect withColor:(NSColor*)color str:(NSString*)str

{

    NSBitmapImageRep *rep = [[NSBitmapImageRep alloc]

                                       initWithBitmapDataPlanes:NULL

                                       pixelsWide:rect.size.width

                                       pixelsHigh:rect.size.height

                                       bitsPerSample:8

                                       samplesPerPixel:4

                                       hasAlpha:YES

                                       isPlanar:NO

                                       colorSpaceName:NSDeviceRGBColorSpace

                                       bitmapFormat:NSAlphaFirstBitmapFormat

                                       bytesPerRow:0

                                       bitsPerPixel:0];

    NSGraphicsContext *gc = [NSGraphicsContext graphicsContextWithBitmapImageRep:rep];

    [NSGraphicsContext setCurrentContext:gc];

    CGContextRef context = [gc graphicsPort];

    CGContextSetFillColorWithColor(context, [color CGColor]);

    CGContextFillRect(context, rect);

    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];

    NSIntegerfontSize = rect.size.width*2/3.0;

    attrs[NSFontAttributeName] = [NSFont systemFontOfSize:fontSize];

    attrs[NSForegroundColorAttributeName] = [NSColor whiteColor];

    CGSizestrSize = [strsizeWithAttributes:attrs];

    [strdrawAtPoint:CGPointMake(rect.size.width/2 - strSize.width/2, (rect.size.height - strSize.height)/2) withAttributes:attrs];

    NSImage *img = [[NSImage alloc] initWithSize:rect.size];

    [imgaddRepresentation:rep];

    returnimg;

}

相关文章

  • Mac默认头像

    // 用户Account的hashCode取绝对值和颜色列表总数取余,即背景色对应的位置。图片上的文字为用户nam...

  • Django库的安装的一些小问题

    一、修改Mac默认python版本: Mac默认的python版本为2.7 自己安装了3.7的版本 Mac在启动,...

  • 【Mac小技巧】Mac电脑更换用户头像8

    想给Mac电脑换一个喜欢的用户头像?苹果电脑的用户头像如何更换? 对于很多Mac小白来说,给自己的Mac换一个可心...

  • 利用原生态NSURLConnection上传用户头像

    看一下界面: 服务器默认是的头像:服务器默认头像.png 1、首先:我们利用导入 #import "AFNetwo...

  • iOS默认头像

    接到一个需求,根据用户账号显示默认头像。背景色根据账号执行一个算法获取到,头像上显示用户名首字。 安卓算法 col...

  • Mac 环境变量配置

    mac一般使用bash作为默认shell,如果安装了oh my sh,则默认使用zshshell。 Mac系统环境...

  • Mac下PHP环境配置

    Mac下httpd+php环境配置 Mac下默认配置了apache2使用默认的apache-httpd来完成配置a...

  • 赚取积分页面跳转

    需要传参数的页面,获取用户ID 判断用户是否设置了头像,没有则显示默认头像

  • windows下git协作换行符问题(LF/CRLF)

    【背景】 mac系统默认的换行符是LF,windows系统默认换行符是CRLF。 当mac上的代码提交到git上,...

  • 更换头像的测试点(app)

    功能: 1,点击头像可以放大观看2,查看头像是否支持放大,缩小3,刚创建账号时是否显示默认头像4,查看头像之后点击...

网友评论

      本文标题:Mac默认头像

      本文链接:https://www.haomeiwen.com/subject/vphazxtx.html