圆形的 imageView在我们项目中一般也经常用到,有种方法可以设置。
方法一:设置圆角
UIImageView *imageheader = [[UIImageView alloc] initWithFrame:CGRectMake(50, 100, 100, 100)];
imageheader.image = [UIImage imageNamed:@"text.jpf"];
[self.view addSubview:imageheader];
imageheader.layer.cornerRadius = imageheader.frame.size.width/2;
//注意:cornerRadius设置为imageView宽度的一半
imageheader.layer.masksToBounds = YES; //隐藏剩余部分
效果图
方法二:配合使用CAShapeLayer和UIBezierPath绘制
UIImageView *imageheader = [[UIImageView alloc] initWithFrame:CGRectMake(80, 100, 100, 100)];
imageheader.image = [UIImage imageNamed:@"text2.jpg"];
[self.view addSubview:imageheader];
CAShapeLayer *shapeLayer = [CAShapeLayer new];
imageheader.layer.mask = shapeLayer;
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(50, 50) radius:50 startAngle:0 endAngle:M_PI*2 clockwise:YES];
shapeLayer.path = bezierPath.CGPath;
效果图: