iOS 實(shí)現(xiàn)圖片圓角的幾種方式

第一種方法:通過設(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];

第二種方法:使用Core Graphics框架畫出一個圓角

@interface UIImage(YYPClip)

- (UIImage *)drawCircleImage;

@end

@implementation UIImage(YYPClip)

- (UIImage *)drawCircleImage {
    CGFloat side = MIN(self.size.width, self.size.height);
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(side, side), false, [UIScreen mainScreen].scale);
    CGContextAddPath(UIGraphicsGetCurrentContext(),
                    [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, side, side)].CGPath);
    CGContextClip(UIGraphicsGetCurrentContext());
    CGFloat X = -(self.size.width - side) / 2.f;
    CGFloat Y = -(self.size.height - side) / 2.f;
    [self drawInRect:CGRectMake(X, Y, self.size.width, self.size.height)];
    CGContextDrawPath(UIGraphicsGetCurrentContext(), kCGPathFillStroke);
    UIImage *output = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return output;
}

@end

//在需要圓角時調(diào)用如下
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    UIImage *img = [[UIImage imageNamed:@"order"] drawCircleImage];
    dispatch_async(dispatch_get_main_queue(), ^{
        view.image = img;
    });
});

第三種方法:使用CAShapeLayer和UIBezierPath設(shè)置圓角

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

以上三種方法來自簡書鏈接 http://www.itdecent.cn/p/e97348f42276
這個鏈接的文章里說第三種方法最好 對內(nèi)存消耗最少,可是在另一篇比較權(quán)威的文章http://www.itdecent.cn/p/57e2ec17585b 《iOS-離屏渲染詳解》里說第三種方法會使用到mask屬性,會離屏渲染,不僅這樣,還曾加了一個 CAShapLayer對象.著實(shí)不可以取。并指出第二種方法比較可取。另外還提出了第四種方法。
第四種方法:使用帶圓形的透明圖片.(需要UI做一個切圖)

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    imageView.image = [UIImage imageNamed:@"美國1.jpeg"];
    UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    imageView1.image = [UIImage imageNamed:@"圓形白邊中空圖"];
    [self.view addSubview:imageView];
    [self.view addSubview:imageView1];

最好最好的方法應(yīng)該是第四種了,雖然比較笨, 但是不會引發(fā)離屏渲染,對內(nèi)存消耗會比較小。

更新:SDWebImage中提供了切圓角圖片的方法,在UIImage + Transform類別里

- (nullable UIImage *)sd_roundedCornerImageWithRadius:(CGFloat)cornerRadius
                                              corners:(SDRectCorner)corners
                                          borderWidth:(CGFloat)borderWidth
                                          borderColor:(nullable UIColor *)borderColor;

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

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

  • 一般我們在iOS開發(fā)的過程中設(shè)置圓角都是如下這樣設(shè)置的。 所有如果要高性能的設(shè)置圓角就需要找另外的方法了。下面是我...
    齊滇大圣閱讀 15,133評論 29 98
  • 一般我們在iOS開發(fā)的過程中設(shè)置圓角都是如下這樣設(shè)置的。 這樣設(shè)置會觸發(fā)離屏渲染,比較消耗性能。比如當(dāng)一個頁面上有...
    zgsddzwj閱讀 660評論 0 1
  • 對圖片進(jìn)行圓角處理會相比于直角,它更加柔和優(yōu)美,是一種很常見的視圖效果,在APP中常用于對用戶頭像的美化,但是設(shè)置...
    打瞌睡de小男孩閱讀 6,156評論 4 22
  • demo地址 一:相對簡便的圓角圖片的實(shí)現(xiàn)方式 由于這樣的處理機(jī)制是GPU在當(dāng)前緩沖區(qū)以外新開辟一個渲染緩沖區(qū)進(jìn)行...
    雷鳴1010閱讀 3,020評論 0 7
  • 前段時間我辦了一張健身卡,從農(nóng)村搬到城市,這是我第一個急切要做的事情,健身,在農(nóng)村里,基礎(chǔ)設(shè)施太差,最多只能去鄉(xiāng)間...
    簡小商閱讀 434評論 0 2

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