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];

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容