日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS设置圆角的4种方法

發布時間:2024/1/18 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS设置圆角的4种方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1 直接layer層的設置(不推薦)
其中的masksToBounds會實現離屏渲染 GPU會在當前屏幕緩沖區開辟一個新的緩沖區進行工作 也就是離屏渲染 這會給我們帶來額外的性能損耗 如果這樣的圓角操作達到一定數量 會觸發緩沖區的頻繁合并和上下文的頻繁切換 性能上宏觀提現是掉幀 不建議使用 iOS9以后系統會判斷 能不產生離屏渲染的就不用了

UIImageView *imgTest = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; imgTest.image = [UIImage imageNamed:@"test.jpg"]; [self.view addSubview:imgTest]; imgTest.layer.cornerRadius = 50; imgTest.layer.masksToBounds = YES;

2 貝塞爾曲線+CoreGraphics(推薦)

//創建新的位圖 //size 新位圖的大小 opaque 透明開關 scale 縮放因子 設置為0 系統自動匹配 UIGraphicsBeginImageContextWithOptions(imgTest.bounds.size, NO, 0); //用貝塞爾曲線畫一個圓形 addClip 進行切割 [[UIBezierPath bezierPathWithRoundedRect:imgTest.bounds cornerRadius:50] addClip]; //開始繪圖 [imgTest drawRect:imgTest.bounds]; imgTest.image = UIGraphicsGetImageFromCurrentImageContext(); //結束畫圖 UIGraphicsEndImageContext();

3?CoreGraphics(推薦)

UIGraphicsBeginImageContextWithOptions(imgTest.bounds.size, NO, 0); //獲取上下文 CGContextRef ctx = UIGraphicsGetCurrentContext(); //設置一個范圍 CGRect rect = CGRectMake(0, 0, imgTest.bounds.size.width, imgTest.bounds.size.height); //給上下文畫一個橢圓 CGContextAddEllipseInRect(ctx, rect); //裁剪 CGContextClip(ctx); //開始繪圖 [imgTest drawRect:imgTest.bounds]; imgTest.image = UIGraphicsGetImageFromCurrentImageContext(); //結束 UIGraphicsEndImageContext();

4 CAShapeLayer+貝塞爾曲線(不推薦)

UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:imgTest.bounds byRoundingCorners:UIRectCornerTopRight|UIRectCornerTopLeft cornerRadii:CGSizeMake(50, 50)]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; //設置切割的范圍和路徑 maskLayer.frame = imgTest.bounds; maskLayer.path = maskPath.CGPath; mgTest.layer.mask = maskLayer; //相比之下第四種更加簡潔 但是有mask 會產生離屏渲染 //還可以規定范圍UIRectCorner /*UIRectCornerTopLeft 上左 UIRectCornerTopRight 上右 UIRectCornerBottomLeft 下左 UIRectCornerBottomRight 下右 UIRectCornerAllCorners 全部 */

?

總結

以上是生活随笔為你收集整理的iOS设置圆角的4种方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。