iOS設置圓角的三種方式
第一種方法:通過設置layer的屬性
最簡單的一種,但是很影響性能,一般在正常的開發(fā)中使用很少.
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框架畫出一個圓角
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設置圓角
首先需要導入<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];
//設置大小
maskLayer.frame = imageView.bounds;
//設置圖形樣子
maskLayer.path = maskPath.CGPath;
imageView.layer.mask = maskLayer;
[self.view addSubview:imageView];
}
這三種方法中第三種最好,對內(nèi)存的消耗最少啊,而且渲染快速。
以上所述是小編給大家介紹的iOS設置圓角的三種方式,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
iOS利用UIScrollView實現(xiàn)圖片的縮放實例代碼
本篇文章主要介紹了iOS利用UIScrollView實現(xiàn)圖片的縮放實例代碼,具有一定的參考價值,有興趣的可以了解一下2017-07-07
在iOS中使用OpenGL ES實現(xiàn)繪畫板的方法
這篇文章主要介紹了在iOS中使用OpenGL ES實現(xiàn)繪畫板的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-03-03
iOS開發(fā) widget構建詳解及實現(xiàn)代碼
這篇文章主要介紹了iOS開發(fā) widget構建詳解的相關資料,并附實例代碼,需要的朋友可以參考下2016-11-11

