Three methods for setting rounded corners for iOS and three methods for ios rounded corners
Method 1: Set layer attributes.
This is the simplest one, but it affects performance. It is rarely used in normal development.
?
1234567 |
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake( 100 , 100 , 100 , 100 )]; // You only need to set two attributes of the layer. // Set the rounded corner imageView.layer.cornerRadius = imageView.frame.size.width / 2 ; // Cut out the excess parts imageView.layer.masksToBounds = YES; [self.view addSubview:imageView]; |
Method 2: Use the UIBezierPath and Core Graphics framework of the besell curve to draw a rounded corner.
?
1234567891011 |
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake( 100 , 100 , 100 , 100 )]; imageView.image = [UIImage imageNamed:@ "1" ]; // Start drawing the imageView UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0 ); // Draw a circular chart using the besell Curve [[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: Use CAShapeLayer and UIBezierPath to set the rounded corner.
#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];}
Among the three methods, the third is the best, with the least memory consumption and fast rendering.
: