美文网首页
OC圆角图片绘制

OC圆角图片绘制

作者: 骑着雅迪小毛驴上班的老瞿 | 来源:发表于2018-04-25 10:46 被阅读0次

    0.方法零
    不推荐使用系统圆角属性设置image圆角。量多时容易导致一些性能问题
    1.方法一(有用送颗❤)

    - (UIImage *)circleImage:(UIImage *)image{
        
        // NO代表透明度
        UIGraphicsBeginImageContextWithOptions(image.size, NO, 1.0);
        
        // 获得上下文
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        
        // 添加一个圆
        CGRect rect2 = CGRectMake(0, 0, image.size.width, image.size.height);
        
        CGContextAddEllipseInRect(ctx, rect2);
        
        // 裁剪
        CGContextClip(ctx);
        
        // 将图片画上去
        [image drawInRect:rect2];
        
        UIImage *image2 = UIGraphicsGetImageFromCurrentImageContext();
        
        // 关闭上下文
        UIGraphicsEndImageContext();
        
        return image2;
        
    }
    

    2.方法二(有用送颗❤)

    - (UIImage *)roundImageClip:(CGRect)rect image:(UIImage *)image{
        // NO代表透明度
        UIGraphicsBeginImageContextWithOptions(rect.size, NO, 1.0);
    
        创建贝萨尔曲线画内切圆
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:50];
        // 设置填充颜色
         [fillColor setFill];
         UIRectFill(rect);
     
        // 裁剪
        [path addClip];
    
        // 将图片画上去
        [image drawInRect:rect];
    
        // 获取裁剪后的图片
        UIImage *iage = UIGraphicsGetImageFromCurrentImageContext();
    
        关闭上下文
        UIGraphicsEndImageContext();
        return iage;
    }
    

    相关文章

      网友评论

          本文标题:OC圆角图片绘制

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