iOS使用UIBezierPath畫圓角


第一種方法:通過設置layer的屬性

這種方法簡單,但是很影響性能,特別是在UIcollectionView中展示大量圓角圖片,一般在正常的開發中使用很少

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
//設置圓角
imageView.layer.cornerRadius = imageView.frame.size.width / 2;
//將多余的部分切掉
imageView.layer.masksToBounds = YES;
[self.view addSubview:imageView];

 第二方法:使用CAShapeLayer和UIBezierPath設置圓角,種最好,對內存的消耗最少,而且渲染快速

    UIImageView *view = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    view.backgroundColor = [UIColor redColor];
    [self.view addSubview:view];
    
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectEdgeLeft | 
UIRectCornerBottomLeft cornerRadii:CGSizeMake(10, 10)]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init]; maskLayer.frame = view.bounds; maskLayer.path = path.CGPath; view.layer.mask = maskLayer;

 UIRectEdgeLeft 可以根據需求設置需要設置圓角的位置。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM