美文网首页
iOS图片圆角

iOS图片圆角

作者: 雷霸龙 | 来源:发表于2020-05-15 10:16 被阅读0次

\color{red}{第三种方式会导致离屏渲染,推荐使用第二种方式}

一:通过设置layer属性,主要是cornerRadius和masksToBounds属性,但是这种方式影响性能,不推荐使用。

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

// swift
        let imgView = UIImageView()
        imgView.frame = CGRect(x: 50, y: 100, width: UIScreen.main.bounds.width - 100, height: 300)
        imgView.image = UIImage(named: "timg.jpeg")
        //开始对imageView进行画图
        UIGraphicsBeginImageContextWithOptions(imgView.bounds.size, false, 1.0)
        //使用贝塞尔曲线画出一个圆形图
        UIBezierPath(roundedRect: imgView.bounds, cornerRadius: 20).addClip()
        imgView.draw(imgView.bounds)
        
        imgView.image = UIGraphicsGetImageFromCurrentImageContext()
        // 结束画图
        UIGraphicsEndImageContext()
        view.addSubview(imgView)



// OC
    UIImageView * imgView = [[UIImageView alloc] init];
    imgView.frame = CGRectMake(50, 100, UIScreen.mainScreen.bounds.size.width - 100, 300);
    imgView.image = [UIImage imageNamed:@"timg.jpeg"];
    //开始对imageView进行画图
    UIGraphicsBeginImageContextWithOptions(imgView.bounds.size, NO, 1.0);
    //使用贝塞尔曲线画出一个圆形图
    [[UIBezierPath bezierPathWithRoundedRect:imgView.bounds cornerRadius:20] addClip];
    [imgView drawRect:imgView.bounds];
    
    imgView.image = UIGraphicsGetImageFromCurrentImageContext();
    //结束画图
    UIGraphicsEndImageContext();
    [self.view addSubview:imgView];

三:使用CAShapeLayer和UIBezierPath设置圆角

// swift
        let imgView1 = UIImageView()
        imgView1.frame = CGRect(x: 50, y: 420, width: UIScreen.main.bounds.width - 100, height: 300)
        imgView1.image = UIImage(named: "timg.jpeg")
        let maskPath = UIBezierPath(roundedRect: imgView1.bounds, byRoundingCorners: .allCorners, cornerRadii: CGSize(width: 20, height: 20))
        let maskLayer = CAShapeLayer()
        // 设置大小
        maskLayer.frame = imgView1.bounds
        // 设置图形样子
        maskLayer.path = maskPath.cgPath
        imgView1.layer.mask = maskLayer
        view.addSubview(imgView1)



// OC
    UIImageView * imgView1 = [[UIImageView alloc] init];
    imgView1.frame = CGRectMake(50, 420, UIScreen.mainScreen.bounds.size.width - 100, 300);
    imgView1.image = [UIImage imageNamed:@"timg.jpeg"];
    UIBezierPath * maskPath = [UIBezierPath bezierPathWithRoundedRect:imgView1.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(20, 20)];
    CAShapeLayer * maskLayer = [[CAShapeLayer alloc] init];
    // 设置大小
    maskLayer.frame = imgView1.bounds;
    // 设置图形样子
    maskLayer.path = maskPath.CGPath;
    imgView1.layer.mask = maskLayer;
    [self.view addSubview:imgView1];

相关文章

  • [iOS] 图像处理:一种高效裁剪图片圆角的算法

    [iOS] 图像处理:一种高效裁剪图片圆角的算法 [iOS] 图像处理:一种高效裁剪图片圆角的算法

  • iOS 圆角图片

    // 开启图形上下文 // 剪裁 //绘制图片 // 从上下文中获取剪裁好的图片 // 关闭图形上下文

  • iOS图片圆角

    一:通过设置layer属性,主要是cornerRadius和masksToBounds属性,但是这种方式影响性能,...

  • Image

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

  • 图片处理

    iOS中图片的加载、圆角、阴影实现方式多种多样,我们需着重考虑性能问题 视图阴影 圆角图片 注意:这种方法能够避免...

  • 2018-07-04

    后台绘制圆角图片 参考链接链接 iOS 离屏渲染优化(Offscreen Render)

  • 关于UIView切圆角的两种方式

    在 iOS App 开发中,切圆角的场景有很多。很多图片或者 UIView 都需要切圆角。 切圆角的方式一般有两种...

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

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

  • iOS-UIImageView圆角设置

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

  • UIImage 处理(I)

    参考资料:iOS绘图 - UIImage的一些简单操作iOS 图片圆角处理及各种“角”的解决方案

网友评论

      本文标题:iOS图片圆角

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