iOS 制作圆形头像图片

1 背景描述:在tableViewCell中制作圆形图片(头像)

2 实现代码:    

        // Image裁剪成圆形,无法直接利用cell内部的imageViewtextLabel,因其是固定位置的

        // 利用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];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值