1 背景描述:在tableViewCell中制作圆形图片(头像)
2 实现代码:
// Image裁剪成圆形,无法直接利用cell内部的imageView和textLabel,因其是固定位置的
// 利用CAShapeLayer可以制作出任意的几何图形,把它作为UIImageView的遮罩,达到把图片做成圆形效果
UIImageView *iView = [[UIImageView alloc] initWithFrame:CGRectMake(255,64,60, 60)];
// 如果filePath中设置了头像图片则获取,没有则使用默认图像
if (filePath !=nil) {
//iView.image = self.imgView.image; //直接赋值
iView.image = [[UIImage alloc]initWithContentsOfFile:filePath]; //从图片路径获取
} else {
iView.image = [UIImage imageNamed:@"loginPic.png"];
}
UIBezierPath * path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(30,30) radius:30 startAngle:0 endAngle:2 * M_PI clockwise:YES];
CAShapeLayer* shape = [CAShapeLayer layer];
shape.path = path.CGPath;
iView.layer.mask = shape;
iView.layer.cornerRadius = 30;
iView.layer.borderWidth = 1;
// iView.layer.borderColor= [[UIColor colorWithRed:170.f green:190.f blue:210.f alpha:1] CGColor];
iView.layer.borderColor= [[UIColor blueColor]CGColor];
[self.view addSubview:iView];