iOS-设置圆角的方法

1.通过设置layer的属性

view.layer.cornerRadius = ??;
view.layer.masksToBounds = YES;

2.使用贝塞尔曲线UIBezierPath和Core Graphics框架画出一个圆角

    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(15, 100, 60, 60)];
    imageView.image = [UIImage imageNamed:@"我的头部BG"];
    // 开启图形上下文
    UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0);
    
    //使用贝塞尔曲线画出一个圆形图
    [[UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:imageView.frame.size.width] addClip];
    [imageView drawRect:imageView.bounds];
    // 从图形上下文中获取圆角图片
    imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    //关闭图形上下文
    UIGraphicsEndImageContext();
    [self.view addSubview:imageView];

3.使用CAShapeLayer和UIBezierPath设置圆角

    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(15, 100, 60, 60)];
    imageView.image = [UIImage imageNamed:@"我的头部BG"];
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:imageView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight cornerRadii:imageView.bounds.size];
    
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
    //设置大小
    
    maskLayer.frame = imageView.bounds;
    //设置形状
    maskLayer.path = maskPath.CGPath;
    imageView.layer.mask = maskLayer;
    [self.view addSubview:imageView];

你可能感兴趣的:(iOS-设置圆角的方法)