iOS設定圓角的三種方式,ios圓角三種方式
第一種方法:通過設定layer的屬性
最簡單的一種,但是很影響效能,一般在正常的開發中使用很少.
?
1234567 |
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake( 100 , 100 , 100 , 100 )]; //只需要設定layer層的兩個屬性 //設定圓角 imageView.layer.cornerRadius = imageView.frame.size.width / 2 ; //將多餘的部分切掉 imageView.layer.masksToBounds = YES; [self.view addSubview:imageView]; |
第二種方法:使用貝茲路徑UIBezierPath和Core Graphics架構畫出一個圓角
?
1234567891011 |
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake( 100 , 100 , 100 , 100 )]; imageView.image = [UIImage imageNamed:@ "1" ]; //開始對imageView進行畫圖 UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0 ); //使用貝茲路徑畫出一個圓形圖 [[UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:imageView.frame.size.width] addClip]; [imageView drawRect:imageView.bounds]; imageView.image = UIGraphicsGetImageFromCurrentImageContext(); //結束畫圖 UIGraphicsEndImageContext(); [self.view addSubview:imageView]; |
第三種方法:使用CAShapeLayer和UIBezierPath設定圓角
#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];}
這三種方法中第三種最好,對記憶體的消耗最少啊,而且渲染快速。
: