UIBezierPath bezier curve to draw rounded corners to the view

1. Draw a circle

-(void)pd_setRadius{ [self.superview layoutIfNeeded]; / / make sure get bounds UIBezierPath * maskPath = [UIBezierPath bezierPathWithRoundedRect: self. The bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:self.bounds.size]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init]; maskLayer.frame = self.bounds; maskLayer.path = maskPath.CGPath; self.layer.mask = maskLayer; }Copy the code

2, draw rounded corners

-(void)pd_setRadius:(float)radius{
    
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(radius, radius)];
    
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = bounds;
    maskLayer.path = maskPath.CGPath;
    
    [self.layer setMask: maskLayer];
}
Copy the code

3, custom draw rounded corners

/ / set to the Angle of view of the above two rounded corners - (void) radius, setTopLeftTopRightCornerRadius: (float) {/ / set the required UIBezierPath * maskPath = rounded corner position and size [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(radius, radius)]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = self.bounds; maskLayer.path = maskPath.CGPath; self.layer.mask = maskLayer; } typepedef NS_OPTIONS(NSUInteger, UIRectCornerTopLeft) {UIRectCornerTopLeft = 1 << 0, UIRectCornerTopRight = 1 << 1, UIRectCornerTopRight = 1 << 1, UIRectCornerTopRight = 1 << 1, UIRectCornerBottomLeft = 1 << 2, UIRectCornerBottomRight = 1 << 3, UIRectCornerAllCorners = ~0UL };Copy the code

Fillet the image directly using drawing techniques

 - (UIImage *)imageWithCornerRadius:(CGFloat)radius size:(CGSize)size {
    CGRect rect = (CGRect){0.f, 0.f, size};
    UIGraphicsBeginImageContextWithOptions(size, NO, UIScreen.mainScreen.scale);
    CGContextAddPath(UIGraphicsGetCurrentContext(), [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius].CGPath);
        CGContextClip(UIGraphicsGetCurrentContext());
    [self drawInRect:rect];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    return image;
}
Copy the code