iOS设置圆角头像

一般情况下,可使用较快速的设置方法:

UIImageView*headerImageView = [[UIImageViewalloc]initWithFrame:CGRectMake(10,10,45,45)];

headerImageView.image= [UIImageimageNamed:@"image.jpeg"];

headerImageView.layer.cornerRadius=90/4;//设置圆角

headerImageView.layer.masksToBounds=true;//利用masksToBounds隐藏不需要渲染的部分

[self.view addSubview:headerImageView];


另外,我们可以用Core Graphics自己画:

@interfaceUIImage (RadioImage)

- (UIImage*)hm_drawRectWithRoundedCorner:(CGFloat)radius SizeToFit:(CGSize)sizeToFit;

@end

#import"UIImage+RadioImage.h"

@implementationUIImage (RadioImage)

- (UIImage*)hm_drawRectWithRoundedCorner:(CGFloat)radius SizeToFit:(CGSize)sizeToFit

{

CGRectrect =CGRectMake(0,0, sizeToFit.width, sizeToFit.height);

UIBezierPath*path = [UIBezierPathbezierPathWithRoundedRect:rectbyRoundingCorners:UIRectCornerAllCornerscornerRadii:CGSizeMake(radius, radius)];

UIGraphicsBeginImageContextWithOptions(rect.size,false, [[UIScreenmainScreen]scale]);

CGContextAddPath(UIGraphicsGetCurrentContext(), path.CGPath);

CGContextClip(UIGraphicsGetCurrentContext());

[selfdrawInRect:rect];

CGContextDrawPath(UIGraphicsGetCurrentContext(),kCGPathFillStroke);

UIImage*outputImage =UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

returnoutputImage;

}

@end


@interfaceUIImageView (RadioImageView)

- (void)hm_addCorner:(CGFloat)radius;

@end

#import"UIImageView+RadioImageView.h"

#import"UIImage+RadioImage.h"

@implementationUIImageView (RadioImageView)

- (void)hm_addCorner:(CGFloat)radius

{

self.image= [self.image hm_drawRectWithRoundedCorner:radiusSizeToFit:self.bounds.size];

}

@end

使用的时候倒入“UIImageView+RadioImageView.h”

UIImageView *headerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10,10,45,45)];

headerImageView.image= [UIImageimageNamed:@"header.jpeg"];

[headerImageView hm_addCorner:30];

[cell.contentView addSubview:headerImageView];

你可能感兴趣的:(iOS设置圆角头像)