利用CAShapeLayer可以制作出任意的几何图形,把它作为UIImageView的遮罩,达到把图片做成圆形效果。
1 //创建个人主页头部的用户头像
2 self.userHead = [[UIImageView alloc]initWithFrame:CGRectMake(10, 35, 80, 80)];
3 self.userHead.image = [UIImage imageNamed:@"start.jpg"];
4
5 //创建圆形遮罩,把用户头像变成圆形
6 UIBezierPath* path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(40, 40) radius:40 startAngle:0 endAngle:2*M_PI clockwise:YES];
7 CAShapeLayer* shape = [CAShapeLayer layer];
8 shape.path = path.CGPath;
9 self.userHead.layer.mask = shape;
10
11 [self addSubview:self.userHead];