iOS开发一行代码解决绘制图片头像(将UILabel/UIView 转化成Image封装了一套方法)

粉色的小强头像为示例:

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];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,136评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,223评论 4 61
  • 本周时间有限,吃老本分享一下关于开关电源的知识。 开关电源应用范围非常广泛,我们PC用的电源(无论是台式机还是笔记...
    TCJ阅读 832评论 1 4
  • 序,前言,附录讲的都是《定位》,这本书英文名叫horsing sense,讲的是如何借力。中文翻译过来,蹭《定位》...
    米达麦亚阅读 976评论 0 0
  • 生活里我们常常会用到符号,比如地上画个脚,就是指示方向,脚型符号最早发现在古希腊,是用来指示妓院怎么走的,现在更抽...
    宅小阳阅读 1,448评论 0 0