//了解切圆角的方式有三种,我们一种一种来看 //圆角的第一种方式:(设置Layer属性) UIImageView *imageView = [[UIImageView alloc]init]; imageView.backgroundColor = [UIColor purpleColor]; imageView.frame = CGRectMake(110, 100, 200, 200); imageView.image = [UIImage imageNamed:@"1"]; //将多余的部分切掉 imageView.layer.masksToBounds = YES; //设置圆角 imageView.layer.cornerRadius = imageView.frame.size.width / 2; [self.view addSubview:imageView];
//圆角的第二种方式:(使用贝塞尔曲线:UIBezierPath和Core Graphics 框架画出一个圆角) UIImageView *imageView = [[UIImageView alloc]init]; imageView.backgroundColor = [UIColor purpleColor]; imageView.frame = CGRectMake(110, 100, 200, 200); imageView.image = [UIImage imageNamed:@"1"]; /** * 对图片进行画图 * * @param size#> 图片的大小 description#> * @param opaque#> 是否透明 description#> * @param scale#> 比例大小 description#> * * @return nil */ UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0); /** * 使用贝塞尔曲线画出一个圆形图 * * @param CGRect 图片路径 * * @return 圆角大小 */ [[UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:imageView.frame.size.width] addClip]; [imageView drawRect:imageView.bounds]; imageView.image = UIGraphicsGetImageFromCurrentImageContext(); // 结束画图 UIGraphicsEndImageContext(); [self.view addSubview:imageView];
// 圆角的第三种方式:(使用CAShapeLayer和UIBezierPath)设置圆角 注:此方法消耗的内存少,渲染快. // 首先导入框架 #import <AVFoundation/AVFoundation.h> UIImageView *imageView = [[UIImageView alloc]init]; imageView.backgroundColor = [UIColor purpleColor]; imageView.frame = CGRectMake(110, 100, 200, 200); imageView.image = [UIImage imageNamed:@"1"]; UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:imageView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:imageView.bounds.size]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init]; //设置大小 maskLayer.frame = imageView.bounds; //设置图形样子 maskLayer.path = path.CGPath; imageView.layer.mask = maskLayer; [self.view addSubview:imageView];
时间: 2024-10-12 10:28:53