Three methods of setting fillet in IOS

Time:2021-4-23

In this article, we share three ways to set fillet on IOS for your reference. The details are as follows

The first method is to set the layer properties

The simplest one, but it affects performance very much. It is seldom used in normal development

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
//You only need to set two properties of the layer
//Set fillet
imageView.layer.cornerRadius = imageView.frame.size.width / 2;
//Cut off the excess
imageView.layer.masksToBounds = YES;
[self.view addSubview:imageView];

The second method: use Bezier curve uibezierpath and core graphics framework to draw a fillet

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
 imageView.image = [UIImage imageNamed:@"1"];
 //Start drawing on ImageView
 UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0);
 //Use Bezier curve to draw a circle
 [[UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:imageView.frame.size.width] addClip];
 [imageView drawRect:imageView.bounds];
 imageView.image = UIGraphicsGetImageFromCurrentImageContext();
 //Finish drawing
 UIGraphicsEndImageContext();
 [self.view addSubview:imageView];

The third method: use cashapelayer and uibezierpath to set the fillet


#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
  
  [super viewDidLoad];
  
  UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 200, 100)];
  imageView.image = [UIImage imageNamed:@"1"];
  UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:imageView.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerTopLeft cornerRadii:CGSizeMake(25, 5)];
  CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
  maskLayer.frame = imageView.bounds;
  maskLayer.path = maskPath.CGPath;
  imageView.layer.mask = maskLayer;
  [self.view addSubview:imageView];
}

The third of the three methods is the best, which consumes the least memory and renders fast.

design sketch:

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.