Mac默认头像

2018-02-07  本文已影响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;

}

上一篇 下一篇

猜你喜欢

热点阅读