iOS設(shè)置圓角的三種方式
第一種方法:通過設(shè)置layer的屬性
最簡單的一種,但是很影響性能,一般在正常的開發(fā)中使用很少.
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)]; //只需要設(shè)置layer層的兩個屬性 //設(shè)置圓角 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進(jìn)行畫圖 UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0); //使用貝塞爾曲線畫出一個圓形圖 [[UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:imageView.frame.size.width] addClip]; [imageView drawRect:imageView.bounds]; imageView.image = UIGraphicsGetImageFromCurrentImageContext(); //結(jié)束畫圖 UIGraphicsEndImageContext(); [self.view addSubview:imageView];
第三種方法:使用CAShapeLayer和UIBezierPath設(shè)置圓角
首先需要導(dǎo)入<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]; //設(shè)置大小 maskLayer.frame = imageView.bounds; //設(shè)置圖形樣子 maskLayer.path = maskPath.CGPath; imageView.layer.mask = maskLayer; [self.view addSubview:imageView]; }
這三種方法中第三種最好,對內(nèi)存的消耗最少啊,而且渲染快速。
以上所述是小編給大家介紹的iOS設(shè)置圓角的三種方式,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
iOS中長條藍(lán)色按鈕(button)實(shí)現(xiàn)代碼
本文通過實(shí)例代碼給大家介紹了iOS中長條藍(lán)色按鈕(button)實(shí)現(xiàn)方法,代碼簡單易懂,非常不錯,具有參考借鑒價值,需要的朋友參考下吧2017-08-08Objective-C學(xué)習(xí)之ARC的實(shí)現(xiàn)方法
自動引用計(jì)數(shù)(Automatic Reference Counting, ARC)把壓在程序員們肩頭的管理內(nèi)存的重?fù)?dān)卸除了不少,更不用說讓跟蹤內(nèi)存泄漏那樣的煩心事也少了很多。下面這篇文章主要給大家介紹了關(guān)于Objective-C學(xué)習(xí)之ARC的實(shí)現(xiàn)方法,需要的朋友可以參考借鑒下。2017-12-12iOS tableView右側(cè)索引視圖狀態(tài)獲取的方法實(shí)例
tableView用于顯示一個垂直滾動的單元格數(shù)(通常為可重復(fù)使用的單元格)組成的視圖,這篇文章主要給大家介紹了關(guān)于iOS tableView右側(cè)索引視圖狀態(tài)獲取的相關(guān)資料,需要的朋友可以參考下2021-07-07詳解使用Xcode進(jìn)行iOS設(shè)備無線調(diào)試
這篇文章主要介紹了詳解使用Xcode進(jìn)行iOS設(shè)備無線調(diào)試,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12IOS 獲取已連接的wifi信息的實(shí)現(xiàn)代碼
這篇文章主要介紹了IOS 獲取已連接的wifi信息的實(shí)現(xiàn)代碼的相關(guān)資料,這里提供實(shí)現(xiàn)代碼幫助大家學(xué)習(xí)理解這部分內(nèi)容,需要的朋友可以參考下2017-08-08