美文网首页
图片处理

图片处理

作者: 卡布哒Q | 来源:发表于2018-05-29 18:28 被阅读0次

头像图片处理,没有图片时将姓名的首字母显示到图片上

/*
NSString *name = @"name";
// 获取姓名首字母
NSString *charStr = [self transformToPinyin:name];
UIImage *img = [self getImage:charStr];
imageV.image = img;
*/

  • (NSString *)transformToPinyin:(NSString *)name {
    NSMutableString *mutableString = [NSMutableString stringWithString:name];
    CFStringTransform((CFMutableStringRef)mutableString, NULL, kCFStringTransformToLatin, false);
    mutableString = (NSMutableString *)[mutableString stringByFoldingWithOptions:NSDiacriticInsensitiveSearch locale:[NSLocale currentLocale]];
    NSString *tempStr = [mutableString stringByReplacingOccurrencesOfString:@"'" withString:@""];
    NSArray *arr = [tempStr componentsSeparatedByString:@" "];
    NSString *s = @"";
    for (NSString *str in arr) {
    s = [s stringByAppendingString:[str substringToIndex:1]];
    }
    s = [s uppercaseString];
    if (s.length >= 2) {
    s = [s substringToIndex:2];
    }
    return s;
    }

  • (UIImage *)getImage:(NSString *)name
    {
    UIColor *color = [self randomColor]; //获取随机颜色
    CGRect rect = CGRectMake(0.0f, 0.0f, 24, 24);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    NSString *headerName = nil;
    if (name.length < 3) {
    headerName = name;
    }else{
    headerName = [name substringFromIndex:name.length-2];
    }
    UIImage *headerimg = [self imageToAddText:img withText:headerName];
    return headerimg;
    }

//随机颜色

  • (UIColor *)randomColor
    {
    CGFloat hue = ( arc4random() % 256 / 256.0 ); //0.0 to 1.0
    CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0,away from white
    CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5;
    return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
    }

//把文字绘制到图片上

  • (UIImage *)imageToAddText:(UIImage *)img withText:(NSString *)text
    {
    //1.获取上下文
    UIGraphicsBeginImageContext(img.size);
    //2.绘制图片
    [img drawInRect:CGRectMake(0, 0, img.size.width, img.size.height)];
    //3.绘制文字
    CGRect rect = CGRectMake(0,2, img.size.width, img.size.height - 8);
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
    style.alignment = NSTextAlignmentCenter;
    //文字的属性
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:15],NSParagraphStyleAttributeName:style,NSForegroundColorAttributeName:[UIColor whiteColor]};
    //将文字绘制上去
    [text drawInRect:rect withAttributes:dic];
    //4.获取绘制到得图片
    UIImage *watermarkImg = UIGraphicsGetImageFromCurrentImageContext();
    //5.结束图片的绘制
    UIGraphicsEndImageContext();

    return watermarkImg;
    }

相关文章

  • Plupload 七牛图片上传(二)

    图片基本处理 七牛提供了一些图片处理方式,比如: **图片基本处理 ** √ **图片瘦身 ** 图片高级处理 图...

  • PPT培训第二天

    一、总结 二、图片处理 1,图片边框 2,图片映像 3,图片柔光等图片处理 4,图片格式刷:其他图片同样处理 三、...

  • iOS 图片上传处理 图片压缩 图片处理

    提到从摄像头/相册获取图片是面向终端用户的,由用户去浏览并选择图片为程序使用。在这里,我们需要过UIImagePi...

  • IOS 图片上传处理 图片压缩 图片处理

    提到从摄像头/相册获取图片是面向终端用户的,由用户去浏览并选择图片为程序使用。在这里,我们需要过UIImagePi...

  • 图片处理

    头像图片处理,没有图片时将姓名的首字母显示到图片上 /*NSString *name = @"name";// ...

  • 图片处理

    图片缩放 图片转码

  • 图片处理

    图片流 前端所说的图片流就是读取本地图片,并在页面使用文件流的方式显示出来。 首先,我们简单说下文件上传的几种方式...

  • 图片处理

    1、旋转2、裁剪3、截取4、平铺 自由拉伸 等比例缩放 根据颜色生成图片 截取某个view视图 文字水印 图片水印...

  • 图片处理

    iOS中图片的加载、圆角、阴影实现方式多种多样,我们需着重考虑性能问题 视图阴影 圆角图片 注意:这种方法能够避免...

  • 图片处理

    图片处理:当拿到一个字符串的时候,不知道是url还是本地路径的时候,怎么正确的拿到字符串代表的图片urlStr:为...

网友评论

      本文标题:图片处理

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