美文网首页iOS开发精华专题UI控件的一些属性iOS开发技能
iOS开发一行代码解决绘制图片头像(将UILabel/UIVie

iOS开发一行代码解决绘制图片头像(将UILabel/UIVie

作者: Courage_SC | 来源:发表于2016-07-27 18:08 被阅读1242次

    粉色的小强头像为示例:

    1869333-9fb97a258a82e83c.jpg

    1、绘制将要转化成图片的UI
    HeadImageView.h

    #import <UIKit/UIKit.h>
    
    @interface HeadImageView : UIView
    
    - (id)initWithFrame:(CGRect)frame BackGroundColor:(UIColor*)backGroundColor Text:(NSString*)text TextColor:(UIColor*)textColor TextFontOfSize:(CGFloat)size;
    @end
    

    HeadImageView.m

    #import "HeadImageView.h"
    
    @implementation HeadImageView
    - (id)initWithFrame:(CGRect)frame BackGroundColor:(UIColor *)backGroundColor Text:(NSString *)text TextColor:(UIColor *)textColor TextFontOfSize:(CGFloat)size{
        self = [super initWithFrame:frame];
        if (self) {
            UIView *backView = [[UIView alloc] initWithFrame:frame];
            backView.backgroundColor = backGroundColor;
            [self addSubview:backView];
            
            UILabel *label = [[UILabel alloc] initWithFrame:frame];
            label.text = text;
            label.textColor = textColor;
            label.backgroundColor = backGroundColor;
            label.textAlignment = 1;
            label.font = [UIFont systemFontOfSize:size];
            [backView addSubview:label];
        }
        return self;
    }
    @end
    

    2、建一个类目文件将UI转化成图片Image
    HeadImage.m

    #import <Foundation/Foundation.h>
    #import "HeadImageView.h"
    
    @interface HeadImage : NSObject
    + (UIImage*)imageWithFrame:(CGRect)frame BackGroundColor:(UIColor*)backGroundColor Text:(NSString*)text TextColor:(UIColor*)textColor TextFontOfSize:(CGFloat)size;
    @end
    

    HeadImage.h

    #import "HeadImage.h"
    
    @implementation HeadImage
    + (UIImage *)imageWithFrame:(CGRect)frame BackGroundColor:(UIColor *)backGroundColor Text:(NSString *)text TextColor:(UIColor *)textColor TextFontOfSize:(CGFloat)size{
    //初始化并绘制UI
        HeadImageView *view = [[HeadImageView alloc] initWithFrame:frame BackGroundColor:backGroundColor Text:text TextColor:textColor TextFontOfSize:size];
    
    //转化成image
        UIGraphicsBeginImageContext(view.bounds.size);
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        [view.layer renderInContext:ctx];
        UIImage* tImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return tImage;
    }
    @end
    

    3、使用方法

    //生成图片Image(封装好(背景颜色、图片大小、字体大小、颜色等),一行代码解决绘制图片头像)
    UIImage *image = [HeadImage imageWithFrame:CGRectMake(0, 0, 125 * 2 * FitWidth, 125 * 2 * FitHeight) BackGroundColor:[UIColor colorWithRed:0.58 green:0.65 blue:0.81 alpha:1.00] Text:_member.name TextColor:[UIColor whiteColor] TextFontOfSize:30 * 2];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 125 * FitHeight, 125 * FitHeight)];
               imageView.image = image;
               [self.view addSubview:imageView];
    

    相关文章

      网友评论

      • 麻辣香锅加特辣:异步绘制是不是更好些
      • 帅狗黑皮668:我的字体也很模糊,我是加了一个border,按照你说的字号放大一倍,样式就给改了
        Courage_SC:什么样的
      • Just_go:我的字体是40号加粗, 出来图片字体有锯齿状, 应该是渲染的问题, 为什么会这样
        Courage_SC:@Just_go 你设置80
      • coco_xk:为什么出来的图片label上的字体很模糊呢?
        geek_Yao:@Courage_SC 放到最大也没用的,解决模糊的办法是
        将UIGraphicsBeginImageContext(rect.size);//太模糊
        改用
        /*
        UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale)
        opaque 透明度,不透明设为YES;
        scale 缩放因子,设0时系统自动设置缩放比例图片清晰;设1.0时模糊
        */
        UIGraphicsBeginImageContextWithOptions(rect.size,NO, 0.0);
        亲测会变得相当清晰
        Courage_SC:@coco_xk 字体放大二倍就好了
      • 08d953fdd3ae:为什么没效果,还报错:<Error>: CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
        Courage_SC:@太乙假人 我不知道你是怎么弄出这个错误的
      • 刺骨寒:可以看一下你的这个封装的demo吗?
        Courage_SC:@刺骨寒 就这两对文件就可以 步骤3使用方法
      • hY_Ramos:没效果啊?
        Courage_SC:@hY_Ramos 怎么会没效果

      本文标题:iOS开发一行代码解决绘制图片头像(将UILabel/UIVie

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