-
@interface UIView (RectCorner)
-
-
@end
-
-
@implementation UIView (RectCorner)
-
- (void)setCornerOnTop {
-
UIBezierPath *maskPath;
-
maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
-
byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight)
-
cornerRadii:CGSizeMake(10.0f, 10.0f)];
-
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
-
maskLayer.frame = self.bounds;
-
maskLayer.path = maskPath.CGPath;
-
self.layer.mask = maskLayer;
-
[maskLayer release];
-
}
-
- (void)setCornerOnBottom {
-
UIBezierPath *maskPath;
-
maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
-
byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight)
-
cornerRadii:CGSizeMake(10.0f, 10.0f)];
-
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
-
maskLayer.frame = self.bounds;
-
maskLayer.path = maskPath.CGPath;
-
self.layer.mask = maskLayer;
-
[maskLayer release];
-
}
-
- (void)setAllCorner {
-
UIBezierPath *maskPath;
-
maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
-
cornerRadius:10.0];
-
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
-
maskLayer.frame = self.bounds;
-
maskLayer.path = maskPath.CGPath;
-
self.layer.mask = maskLayer;
-
[maskLayer release];
-
}
-
- (void)setNoneCorner{
-
self.layer.mask = nil;
-
}
-
-
@end
-----------------------------
2,这是给UIImageView的image添加圆角的方法(如果是加载网络图片必须等图片加载完成后调用):
- (UIImageView *)imageWithRoundedCornersSize:(float)cornerRadius
{
UIImageView *imageView = self;
UIImage * original = self.image;
// Begin a new image that will be the new image with the rounded corners
// (here with the size of an UIImageView)
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, [UIScreen mainScreen].scale); //[UIScreen mainScreen].scale
// Add a clip before drawing anything, in the shape of an rounded rect
[[UIBezierPath bezierPathWithRoundedRect:imageView.bounds
cornerRadius:cornerRadius] addClip];
// Draw your image
[original drawInRect:imageView.bounds];
// Get the image, here setting the UIImageView image
imageView.image = UIGraphicsGetImageFromCurrentImageContext();
// Lets forget about that we were drawing
UIGraphicsEndImageContext();
return imageView;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
设置UIView圆角 cornerRadius 圆角有性能问题,用贝塞尔曲线代替
原文:http://blog.csdn.net/shaobo8910/article/details/46779259