Three Ways of Setting Fillet in iOS

  • 2021-11-30 01:41:06
  • OfStack

Method 1: By setting the properties of layer

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


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

Method 2: Draw a rounded corner using Bessel curves UIBezierPath and Core Graphics frames


 UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
  imageView.image = [UIImage imageNamed:@"1"];
  // Begin to be right imageView Draw a picture 
  UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0);
  // Use the Bessel curve to draw 1 A circular graph 
  [[UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:imageView.frame.size.width] addClip];
  [imageView drawRect:imageView.bounds];
  imageView.image = UIGraphicsGetImageFromCurrentImageContext();
   // End drawing 
  UIGraphicsEndImageContext();
  [self.view addSubview:imageView];

Method 3: Set fillet using CAShapeLayer and UIBezierPath

You first need to import < AVFoundation/AVFoundation.h >


#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
  [super viewDidLoad];
  UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
  imageView.image = [UIImage imageNamed:@"1"];
  UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:imageView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:imageView.bounds.size];
  CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
  // Setting Size 
  maskLayer.frame = imageView.bounds;
  // Set the graphic appearance 
  maskLayer.path = maskPath.CGPath;
  imageView.layer.mask = maskLayer;
  [self.view addSubview:imageView];
}

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


Related articles: