美文网首页iOS 知识点
iOS-利用贝塞尔曲线设置圆角

iOS-利用贝塞尔曲线设置圆角

作者: linbj | 来源:发表于2018-01-06 16:00 被阅读647次
    /**
     裁剪图片
     
     @param radius 半径
     @param corners 角
     @param borderWidth 线宽值大于矩形的一半宽度或高度夹适当宽度的一半或高度。
     @param borderColor 线颜色
     @param borderLineJoin 线转交类型
     @return iamge
     */
    - (UIImage *)imageByRoundCornerRadius:(CGFloat)radius
                                  corners:(UIRectCorner)corners
                              borderWidth:(CGFloat)borderWidth
                              borderColor:(UIColor *)borderColor
                           borderLineJoin:(CGLineJoin)borderLineJoin {
        
        // 判断传入的角是哪种
        if (corners != UIRectCornerAllCorners) {
            UIRectCorner tmp = 0;
            if (corners & UIRectCornerTopLeft) tmp |= UIRectCornerBottomLeft;
            if (corners & UIRectCornerTopRight) tmp |= UIRectCornerBottomRight;
            if (corners & UIRectCornerBottomLeft) tmp |= UIRectCornerTopLeft;
            if (corners & UIRectCornerBottomRight) tmp |= UIRectCornerTopRight;
            corners = tmp;
        }
        
        UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
        // 获取当前上下文
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
        // 缩放因子设置为x=1 y= -1
        CGContextScaleCTM(context, 1, -1);
        // 修改xy值,x不变 y上移rect.size.height高度
        CGContextTranslateCTM(context, 0, -rect.size.height);
        
        CGFloat minSize = MIN(self.size.width, self.size.height);
        
        // 当线宽小于 长宽之间较小的那个的一半的时候 利用UIBezierPath裁剪出需要的范围
        if (borderWidth < minSize / 2) {
            UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectInset(rect, borderWidth, borderWidth) byRoundingCorners:corners cornerRadii:CGSizeMake(radius, borderWidth)];
            [path closePath];
            
            CGContextSaveGState(context);
            // 裁剪
            [path addClip];
            CGContextDrawImage(context, rect, self.CGImage);
            CGContextRestoreGState(context);
        }
        
        // 下面判断都满足的情况下去绘制边框线
        if (borderColor && borderWidth < minSize / 2 && borderWidth > 0) {
            CGFloat strokeInset = (floor(borderWidth * self.scale) + 0.5) / self.scale;
            CGRect strokeRect = CGRectInset(rect, strokeInset, strokeInset);
            CGFloat strokeRadius = radius > self.scale / 2 ? radius - self.scale / 2 : 0;
            UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:strokeRect byRoundingCorners:corners cornerRadii:CGSizeMake(strokeRadius, borderWidth)];
            [path closePath];
            
            path.lineWidth = borderWidth;
            path.lineJoinStyle = borderLineJoin;
            [borderColor setStroke];
            [path stroke];
        }
        
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    }
    

    YYCategories

    相关文章

      网友评论

        本文标题:iOS-利用贝塞尔曲线设置圆角

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