- @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;
}