iOS設定圓角的常用方法

來源:互聯網
上載者:User

標籤:ima   設定圖   pat   with   需要   from   drawrect   效能   rom   

//第一種方法:最常用的方法,但是效能最差

    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];

    imageView.image = [UIImage imageNamed:@"晚起的螞蟻.jpg"];

    //只需要設定layer層的兩個屬性

    //設定圓角

    imageView.layer.cornerRadius = imageView.frame.size.width / 2;

    //將多餘的部分切掉

    imageView.layer.masksToBounds = YES;

    [self.view addSubview:imageView];

    

    

    //第二種方法:使用貝茲路徑UIBezierPath和Core Graphics架構畫出一個圓角,效能比第一種高很多(有人說100多倍,具體是多少我也不知道)

    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];

    imageView.image = [UIImage imageNamed:@"晚起的螞蟻.jpg"];

    //開始對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>

    //對記憶體的消耗最少啊,而且渲染快速。

    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];

    imageView.image = [UIImage imageNamed:@"晚起的螞蟻.jpg"];

    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];

iOS設定圓角的常用方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.