iOS--切圓角

切圓角一直是個(gè)老生長談問題,為什么呢?就是因?yàn)樾蕟栴}選擇一個(gè)高效的渲染方式是關(guān)鍵。
*注:現(xiàn)在我們不用太擔(dān)心這個(gè)問題了從9.0以后所有的切圓角的方案都不會(huì)產(chǎn)生離屏渲染。

下面我們說說切圓角都有哪些方式:

  • 設(shè)置視圖的layer屬性:
    //設(shè)置視圖layer層的兩個(gè)屬性
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 30, 100, 100)];
    imageView.image = [UIImage imageNamed:@"head.png"];
    //設(shè)置圓角半徑
    imageView.layer.cornerRadius = 15.0f;
    //將多余的部分切掉
    imageView.layer.masksToBounds = YES;
    
    [self.view addSubview:imageView];

效果圖:


connerRadius.jpg

優(yōu)點(diǎn):方便快捷、代碼簡單。
缺點(diǎn):8.0會(huì)有離屏渲染效率低下,但是9.0做了優(yōu)化以后均是主屏渲染。

  • UIImage切圓角
- (void)imageView:(UIImageView *)imageView cornerRadius:(CGFloat)radius
{
    //開始對(duì)imageView進(jìn)行畫圖
    UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, [UIScreen mainScreen].scale);
    //使用貝塞爾曲線畫出一個(gè)圓形圖
    [[UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:radius] addClip];
    [imageView drawRect: imageView.bounds];
    
    imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    //結(jié)束畫圖
    UIGraphicsEndImageContext();
}

效果圖:


roundedRect.jpg

優(yōu)點(diǎn):不會(huì)產(chǎn)生離屏渲染
系統(tǒng):8.0以后均不會(huì)產(chǎn)生離屏渲染。

  • 通過疊加圖層遮擋方式

此方法就是在要添加圓角view上疊加一個(gè)中間鏤空透明的視圖,只對(duì)圓角部分進(jìn)行遮擋,不過同時(shí)也需要遮擋顏色和view背景色一致才行。
原圖片:


headImage.png
masklayer.png
    self.view.backgroundColor = [UIColor redColor];
    
    UIImageView *headImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"headImage.png"]];
    headImageView.center = CGPointMake(100, 400);
    headImageView.size = CGSizeMake(60, 60);
    [self.view addSubview:headImageView];
    
    UIImage *image = [UIImage imageNamed:@"masklayer.png"];
    UIEdgeInsets insets = UIEdgeInsetsMake(image.size.height/2, image.size.width/2, image.size.height/2, image.size.width/2);
    image = [image resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.x = 0;
    imageView.y = 0;
    imageView.width = headImageView.width;
    imageView.height = headImageView.height;
    [headImageView addSubview:imageView];

合成后圖片:


complex.jpg

優(yōu)點(diǎn):此方法也是最優(yōu)解,沒有離屏渲染,沒有額外的CPU計(jì)算。
缺點(diǎn):應(yīng)用范圍有限。

  • 通過賽貝爾曲線畫出鏤空layer覆蓋到視圖上
    self.view.backgroundColor = [UIColor greenColor];
    
    UIImageView *headImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Image_head"]];
    headImageView.center = CGPointMake(100, 400);
    headImageView.size = CGSizeMake(60, 60);
    [self.view addSubview:headImageView];
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, headImageView.width, headImageView.height) cornerRadius:0];
    [path appendPath:[[UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, headImageView.width, headImageView.height) cornerRadius:20] bezierPathByReversingPath]];
    shapeLayer.path = path.CGPath;
    shapeLayer.lineWidth = 0;
    shapeLayer.strokeColor = [UIColor whiteColor].CGColor;
    shapeLayer.fillColor = [UIColor whiteColor].CGColor;
    shapeLayer.contentsScale = 20;
    shapeLayer.lineCap = kCALineCapRound;
    shapeLayer.strokeStart = 0.0f;
    shapeLayer.strokeEnd = 0.1f;
    [headImageView.layer addSublayer:shapeLayer];

效果圖:


UNADJUSTEDNONRAW_thumb_667.jpg

優(yōu)點(diǎn):效率最高、內(nèi)存消耗最少、不會(huì)產(chǎn)生離屏渲染、相對(duì)圖層疊加減少了一張覆蓋層圖片。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 關(guān)于imageView以如下這種最“普通”的切圓角的方法,網(wǎng)上盛傳,這樣會(huì)影響性能,認(rèn)識(shí)不夠深,不能理解,百度了到...
    一天卡卡閱讀 401評(píng)論 2 1
  • 使用frame布局,是完全沒問題的,如果想用masonry,就需要讓約束布局生效,讓視圖先渲染出來就可以了,一般做...
    nadou23閱讀 8,732評(píng)論 5 5
  • 今天接著看《人民的名義》,對(duì)李達(dá)康這個(gè)角色感覺挺有意思的。 首先從目前看的劇情分析看,李達(dá)康這個(gè)角色主要在于對(duì)權(quán)利...
    丿子木丨閱讀 201評(píng)論 0 0
  • 今晚聽了一個(gè)多小時(shí)的寫作入門課程,老師主要講的是寫文案的思維:競爭思維、產(chǎn)品思維、用戶思維、銷售思維,以及思維的運(yùn)...
    月落April閱讀 1,723評(píng)論 0 1
  • 描述 在 Android 系統(tǒng)中,廣播(Broadcast)是在組件之間傳播數(shù)據(jù)的一種機(jī)制,這些組件可以位于不同的...
    pkqgo閱讀 1,678評(píng)論 1 2

友情鏈接更多精彩內(nèi)容