如何给一个imageview 增加圆角(摘要)
一般情况下给 UIImageView或者说 UIKit的控件添加圆角都是改变clipsToBounds和layer.cornerRadius,这样大约两行代码就可以解决这个问题.但是,这样使用这样的方法会强制 Core Animation 提前渲染屏幕的离屏绘制,而离屏绘制就会为性能带来负面影响.
我们也可以使用另一种比较复杂的方式来为图片添加圆角,这里就用到了贝塞尔曲线.
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 100,100)];
imageView.center = CGPointMake(200,300);
UIImage *anotherImage = [UIImage imageNamed:@"image"];
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO,1.0);
[[UIBezierPath bezierPathWithRoundedRect:imageView.bounds
cornerRadius:50] addClip];
[anotherImage drawInRect:imageView.bounds];
imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.view addSubview:imageView];
***************我是分割线****************
只加上面两个圆角
CGRect rect = view.bounds;
CGSize radio = CGSizeMake(5, 5);//圆角尺寸
UIRectCorner corner = UIRectCornerTopLeft|UIRectCornerTopRight;//这只圆角位置
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corner cornerRadii:radio];
CAShapeLayer *masklayer = [[CAShapeLayer alloc]init];//创建shapelayer
masklayer.frame = view.bounds;
masklayer.path = path.CGPath;//设置路径
view.layer.mask = masklayer;
这里摘自一篇很有质量的博文,ios 面试相关:http://draveness.me/guan-yu-xie-ios-wen-ti-de-jie-da/