
圖文無(wú)關(guān).png
現(xiàn)在幾乎每一個(gè)app都會(huì)有登錄注冊(cè),有登陸注冊(cè)就會(huì)有頭像。
為了方便調(diào)用下面封裝了一下個(gè)人頭像的代碼,大??衫@道_。
個(gè)人頭像有以下幾個(gè)屬性:
/** 頭像邊框顏色 (默認(rèn)白色)*/
@property (nonatomic, strong)UIColor *strokeColor;
/** 頭像邊框?qū)挾?(默認(rèn)2.0)*/
@property (nonatomic, assign)CGFloat strokeWidth;
/** 原始頭像占位圖片 */
@property (nonatomic, strong)UIImage *originalImage;
/** 是否需要頭像陰影 (默認(rèn)無(wú))*/
@property (nonatomic, assign)BOOL needShadow;
/** 頭像 */
@property (nonatomic, assign)CircleHeadViewContentType contentType;
總之封裝的時(shí)候 就一個(gè)思想 把他當(dāng)做一個(gè)對(duì)象來(lái)考慮。
下面是源碼(直接復(fù)制粘貼即可使用)??
#import <UIKit/UIKit.h>
typedef NS_ENUM(NSInteger, CircleHeadViewContentType) {
CircleHeadViewContentResize,
CircleHeadViewContentResizeAspect,
CircleHeadViewContentResizeAspectFill,
};
@interface ZJCircleHeadView : UIView
/** 頭像邊框顏色 (默認(rèn)白色)*/
@property (nonatomic, strong)UIColor *strokeColor;
/** 頭像邊框?qū)挾?(默認(rèn)2.0)*/
@property (nonatomic, assign)CGFloat strokeWidth;
/** 原始頭像占位圖片 */
@property (nonatomic, strong)UIImage *originalImage;
/** 是否需要頭像陰影 (默認(rèn)無(wú))*/
@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) {
//默認(rèn) 沒(méi)有頭像陰影
self.needShadow = NO;
//默認(rèn)邊框顏色 白色
self.strokeColor = [UIColor whiteColor];
//默認(rèn)邊框?qū)挾?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) {
// 創(chuàng)建陰影層
[self createShadowLayer:bounds cornerRadius:cornerRadius];
}
// 創(chuàng)建照片層
[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];

頭像效果.png