个人头像

个人头像_第1张图片
图文无关.png

现在几乎每一个app都会有登录注册,有登陆注册就会有头像。
为了方便调用下面封装了一下个人头像的代码,大牛可绕道_

个人头像有以下几个属性:

/** 头像边框颜色 (默认白色)*/
@property (nonatomic, strong)UIColor *strokeColor;
/** 头像边框宽度 (默认2.0)*/
@property (nonatomic, assign)CGFloat strokeWidth;
/** 原始头像占位图片 */
@property (nonatomic, strong)UIImage *originalImage;
/** 是否需要头像阴影 (默认无)*/
@property (nonatomic, assign)BOOL needShadow;
/** 头像 */
@property (nonatomic, assign)CircleHeadViewContentType contentType;

总之封装的时候 就一个思想 把他当做一个对象来考虑。
下面是源码(直接复制粘贴即可使用)

#import 

typedef NS_ENUM(NSInteger, CircleHeadViewContentType) {

    CircleHeadViewContentResize,
    CircleHeadViewContentResizeAspect,
    CircleHeadViewContentResizeAspectFill,
};

@interface ZJCircleHeadView : UIView
/** 头像边框颜色 (默认白色)*/
@property (nonatomic, strong)UIColor *strokeColor;
/** 头像边框宽度 (默认2.0)*/
@property (nonatomic, assign)CGFloat strokeWidth;
/** 原始头像占位图片 */
@property (nonatomic, strong)UIImage *originalImage;
/** 是否需要头像阴影 (默认无)*/
@property (nonatomic, assign)BOOL needShadow;
/** 头像 */
@property (nonatomic, assign)CircleHeadViewContentType contentType;

- (void)setCircleImage:(UIImage *)image;

@end
#import "ZJCircleHeadView.h"

@implementation ZJCircleHeadView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //默认 没有头像阴影
        self.needShadow = NO;
        //默认边框颜色 白色
        self.strokeColor = [UIColor whiteColor];
        //默认边框宽度 2.0
        self.strokeWidth = 2.0;
        self.contentType = CircleHeadViewContentResize;
    }
    return self;
}

- (void)setCircleImage:(UIImage *)image
{
    
    self.originalImage = image;
    
    CGRect bounds = CGRectMake(0, 0, self.bounds.size.height, self.bounds.size.height);
    CGFloat cornerRadius = self.bounds.size.height/2;
    
    if (self.needShadow) {
        // 创建阴影层
        [self createShadowLayer:bounds cornerRadius:cornerRadius];
    }
    // 创建照片层
    [self createImageLayer:bounds cornerRadius:cornerRadius];
}

- (void)createShadowLayer:(CGRect)bounds cornerRadius:(CGFloat)cornerRadius
{
    CALayer *layerShadow = [[CALayer alloc]init];
    layerShadow.bounds = bounds;
    layerShadow.position = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
    layerShadow.cornerRadius = cornerRadius;
    layerShadow.shadowColor = [UIColor grayColor].CGColor;
    layerShadow.shadowOffset = CGSizeMake(2, 2);
    layerShadow.shadowOpacity = 1;
    layerShadow.borderColor = self.strokeColor.CGColor;
    layerShadow.borderWidth = self.strokeWidth;
    [self.layer addSublayer:layerShadow];
}

- (void)createImageLayer:(CGRect)bounds cornerRadius:(CGFloat)cornerRadius
{
    CALayer *layer = [[CALayer alloc]init];
    layer.bounds = bounds;
    layer.position = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
    layer.backgroundColor = [UIColor blackColor].CGColor;
    layer.cornerRadius = cornerRadius;
    layer.masksToBounds = YES;
    layer.borderColor = self.strokeColor.CGColor;
    layer.borderWidth = self.strokeWidth;
    layer.contents = (id)self.originalImage.CGImage;
    [self.layer addSublayer:layer];
    switch (self.contentType) {
        case CircleHeadViewContentResize:
        {
            layer.contentsGravity = kCAGravityResize;
        }
            break;
        case CircleHeadViewContentResizeAspect:
        {
            layer.contentsGravity = kCAGravityResizeAspect;
        }
            break;
        case CircleHeadViewContentResizeAspectFill:
        {
            layer.contentsGravity = kCAGravityResizeAspectFill;
        }
            break;
            
        default:
            break;
    }
}

@end

使用如下

    //头像
    _headView = [[ZJCircleHeadView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
    _headView.center = CGPointMake(centerView.frame.size.width / 2, _headView.frame.size.height / 2);
    _headView.needShadow = YES;
    _headView.strokeWidth = 2.0;
    _headView.strokeColor = [UIColor whiteColor];
    _headView.contentType = CircleHeadViewContentResizeAspectFill;
    [_headView setCircleImage:[UIImage imageNamed:@"head"]];
    [centerView addSubview:_headView];
个人头像_第2张图片
头像效果.png

你可能感兴趣的:(个人头像)