美文网首页
iOS 设置图片圆角的三种方式

iOS 设置图片圆角的三种方式

作者: 您079 | 来源:发表于2018-05-17 18:22 被阅读0次

方式一:通过 layer 设置圆角
最简单的一种,但是影响性能,一般在正常的开发中使用很少

    // 创建图片框
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    // 添加图片
    imageView.image = [UIImage imageNamed:@"104.jpg"];
    // 使用 layer 设置圆角
    imageView.layer.cornerRadius = 50;
    imageView.layer.masksToBounds = YES;
    [self.view addSubview:imageView];

方式二:使用贝塞尔曲线UIBezierPath和Core Graphics框架画出一个圆角

    // 创建图片框
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    // 添加图片
    imageView.image = [UIImage imageNamed:@"104.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设置圆角
这种方式最好,内存的消耗最少,而且渲染快速

    // 创建图片框
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    // 添加图片
    imageView.image = [UIImage imageNamed:@"104.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-UIImageView圆角设置

    iOS开发中图片圆角设置是最常见的需求,圆角符合人类视觉安全体验,让人感觉舒适,设置圆角也是非常简单,有五种方式来...

  • iOS 设置图片圆角的三种方式

    方式一:通过 layer 设置圆角最简单的一种,但是影响性能,一般在正常的开发中使用很少 方式二:使用贝塞尔曲线U...

  • Image

    直接圆角图片 设置圆角图片度数 设置圆角图片带灰色圆角边框 设置圆角图片带灰色圆角边框带阴影

  • Flutter知识点总结一

    一、设置图片的圆角 由于图片不能直接设置圆角,所以我们需要采用其它方式来设置: 1、使用ClipRRect进行设置...

  • flutter 设置圆角图片三种方式

    1.ClipRRect可设置添加默认图片 2.BoxDecoration 3.ShapeDecoration

  • iOS设置圆角的三种方式

    第一种方法:通过设置layer的属性最简单的一种,但是很影响性能,一般在正常的开发中使用很少. 或者在xib中设置...

  • iOS设置圆角的三种方式

  • iOS设置圆角的三种方式

    转载:https://www.cnblogs.com/mafeng/p/5672528.html 这三种方法中第三...

  • iOS设置圆角的三种方式

    转发简书文章:文章地址:http://www.jianshu.com/p/e97348f42276简书作者:FFIB

  • iOS设置圆角的三种方式

    1:通过设置layer属性:最简单的一种,但是很影响性能,会造成离屏渲染,一般在开发中很少使用。 UIImageV...

网友评论

      本文标题:iOS 设置图片圆角的三种方式

      本文链接:https://www.haomeiwen.com/subject/elridftx.html