第一种方式:通过设置控件的layer属性

该方法是iOS实现圆角的方法中最简单的一种,比较影响性能

实现代码如下所示:

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
//    设置圆角
    imageView.layer.cornerRadius = imageView.frame.size.width/2;
//    将多余的部分剪切掉
    imageView.layer.masksToBounds = YES;
    [self.view addSubview:imageView];

如果要给控件设置边框可以设置layer的以下两个属性

//    设置边框 边框的宽度为10
    imageView.layer.borderWidth = 10;
//    设置边框的颜色  红色
    imageView.layer.borderColor = [UIColor redColor].CGColor;

第二种方式:使用贝塞尔曲线UIBezierPath和Core Graphics框架画出一个圆角

实现代码如下所示:

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    imageView.image =[ UIImage imageNamed:@"logo1"];
    //    开始对imageView进行画圆
    UIGraphicsBeginImageContextWithOptions(imageView.frame.size, NO, 1.0);
    //    使用贝塞尔曲线画出一个圆形图
    [[UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:imageView.bounds.size.width] addClip];
    [imageView drawRect:imageView.bounds];
    imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    //结束画图
    UIGraphicsEndImageContext();
    [self.view addSubview:imageView];

第三种方式:使用

CAShapeLayer和贝塞尔曲线UIBezierPath

实现代码如下所示:

UIImageView *Image = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    Image.image = [UIImage imageNamed:@"logo1"];
    UIBezierPath *MaskPath = [UIBezierPath bezierPathWithRoundedRect:Image.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:Image.bounds.size];
    CAShapeLayer *MaskLayer = [[CAShapeLayer alloc]init];
//    这是大小
    MaskLayer.frame = Image.bounds;
//    设置图形的样子
    MaskLayer.path = MaskPath.CGPath;
    Image.layer.mask = MaskLayer;
    [self.view addSubview:Image];

注意:第三种实现方式在使用的时候要导入AVFoundation.framework,然后导入头文件

#import <AVFoundation/AVFoundation.h>

以上三种实现圆角的方式中,第三种方式对内存的消耗最少。

在xib中对实现控件的圆角设置

第一步:选择要设置为圆角的控件。操作如下图所示:

ios 高性能圆角 iphone圆角_圆角

第二步:添加KeyPath

ios 高性能圆角 iphone圆角_iOS_02

设置完成之后如下图所示:

ios 高性能圆角 iphone圆角_xib中设置圆角_03

控件的KeyPath设置完成之后并不会立刻在Xib中显示为圆角,只有当程序运行的时候才会显示出圆角