iOS给图片设置圆角

作者: YanniLiu | 来源:发表于2016-08-10 11:03 被阅读95次

<h1 align = "center">iOS给图片设置圆角</h1>

通过设置layer的属性(最常用的方法)

最快速,但是影响性能,代码如下

 
 UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
  imageView.backgroundColor = [UIColor redColor];
  //设置layer层的两个属性
  //设置圆角
  imageView.layer.cornerRadius = imageView.frame.size.width / 2;
  //将多余的部分切掉,这句话一定要有,不然有时候显示不出来
  imageView.layer.masksToBounds = YES;
  [self.view addSubview:imageView];

通过CAShapeLayer和贝塞尔曲线UIBezierPath画圆角图片


UIImageView *imageShape = [[UIImageView alloc]initWithFrame:CGRectMake(100, 360, 100, 100)];
    imageShape.image = [UIImage imageNamed:@"01.jpg"];
    imageShape.backgroundColor = [UIColor purpleColor];
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:imageShape.bounds
                                                   byRoundingCorners:UIRectCornerAllCorners
                                                         cornerRadii:imageShape.bounds.size];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
    maskLayer.frame = imageShape.bounds;
    maskLayer.path = maskPath.CGPath;
    imageShape.layer.mask = maskLayer;
    [self.view addSubview:imageShape];

用UIBezier曲线和Core Graphics(推荐)

   UIImageView * imageBezier = [[UIImageView alloc]initWithFrame:CGRectMake(100, 220, 100, 100)];
     imageBezier.image = [UIImage imageNamed:@"01.jpg"];
    imageBezier.backgroundColor = [UIColor blueColor];
    //开始对imageView进行画图
    UIGraphicsBeginImageContextWithOptions(imageBezier.bounds.size, NO, 1.0);
    //使用贝塞尔曲线画出一个圆形图
    UIBezierPath * path =  [UIBezierPath bezierPathWithRoundedRect:imageBezier.bounds cornerRadius:imageBezier.frame.size.width] ;
    [path addClip];
    [imageBezier drawRect:imageBezier.bounds];
    
    imageBezier.image = UIGraphicsGetImageFromCurrentImageContext();
    //结束画图
    UIGraphicsEndImageContext();
    [self.view addSubview:imageBezier];


PS:这个demo我已经上传到了我的github上,有兴趣的朋友可以下载看看.

相关文章

  • iOS给图片设置圆角

    iOS给图片设置圆角 通过设置layer的属性(最常用的方法) 最快速,但是影响性能,代码如下 通过CAShape...

  • Image

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

  • iOS:关于给图片设置圆角

    平时开发中给图片设置圆角都是 self.iconImage.layer.cornerRadius = 20; se...

  • iOS开发给图片设置圆角的正确姿势2019-02-12

    在iOS开发中经常会用到给一组图片设置圆角,尤其是设置一个ImageView的左上角和右上角的圆角,那么这种问题应...

  • iOS设置圆角图片

    1.实现方式 简易方式: CoreGraphics剪裁方式 2.性能测试 利用Instruments->Core ...

  • iOS设置图片圆角

    PS:记录自己工作学习中的一些知识; 一、UIImageView iOS9之后:UIImageView使用以下方法...

  • iOS-UIImageView圆角设置

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

  • iOS设置圆角的四种方法

    原文iOS设置圆角的四种方法iOS设置圆角的方法及指定圆角的位置 一、设置CALayer的cornerRadius...

  • iOS设置圆角过量 渲染 卡顿问题

    UILabel处理 图片处理 参考文献iOS设置圆角的四种方法

  • 同时设置圆角和阴影

    单独给图片、按钮设置圆角或者阴影效果比较简单,但是如果这两个同时设置、最后再设置上背景图片,就会发现不是圆角没了,...

网友评论

    本文标题:iOS给图片设置圆角

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