画三条边 的 边框与圆角

// 画边框 (分为左半部分和右半部分,每次画一部分)

  • (UIImage *)getEdgeLineImageWithType:(ELJSecondhandHomePageSceneFrameType)type {
    NSInteger width = self.width;
    NSInteger height = self.height;
    NSInteger cornerRadius = 5;
    NSInteger lineWidth = 1;
    // 沿着边框画,会有一半的线宽画到画板外部,留出最小距离
    CGFloat minSpace = lineWidth / 2.0;

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(self.width, self.height), NO, [UIScreen mainScreen].scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    //设置颜色
    UIColor *lineColor = [UIColor lj_colorWithHex:0xededed];
    [lineColor set];
    //画线
    CGMutablePathRef path = CGPathCreateMutable();
    if (type == ELJSecondhandHomePageSceneFrameTypeLeft) { // 左侧
    CGPathMoveToPoint(path, NULL, width, minSpace);
    CGPathAddLineToPoint(path, NULL, cornerRadius + minSpace, minSpace);
    // 画左上角圆角
    CGPathAddArc(path, NULL, cornerRadius + minSpace, cornerRadius + minSpace, cornerRadius, M_PI_2 * 3, M_PI, true);
    CGPathAddLineToPoint(path, NULL, minSpace, height - cornerRadius - minSpace);
    // 画左下角圆角
    CGPathAddArc(path, NULL, cornerRadius + minSpace, height - cornerRadius - minSpace, cornerRadius, M_PI, M_PI_2, true);
    CGPathAddLineToPoint(path, NULL, width, height - minSpace);
    } else { // 右侧
    CGPathMoveToPoint(path, NULL, 0, minSpace);
    CGPathAddLineToPoint(path, NULL, width - cornerRadius - minSpace, minSpace);
    // 画右上角圆角
    CGPathAddArc(path, NULL, width - cornerRadius - minSpace, cornerRadius + minSpace, cornerRadius, M_PI_2 * 3, 0, false);
    CGPathAddLineToPoint(path, NULL, width, height - cornerRadius - minSpace);
    // 画右下角圆角
    CGPathAddArc(path, NULL, width - cornerRadius - minSpace, height - cornerRadius - minSpace, cornerRadius, 0, M_PI_2, false);
    CGPathAddLineToPoint(path, NULL, 0, height - minSpace);
    }

    CGContextSetLineWidth(context, lineWidth);
    CGContextAddPath(context, path);
    CGContextStrokePath(context);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    CFRelease(path);
    return image;
    }

你可能感兴趣的:(画三条边 的 边框与圆角)