4

在下面,您会看到每次调用 drawMulticolorLine 时都会绘制一条多色线的代码。

    void drawMulticolorLine
    {
        CGContextBeginPath(secondaryContext);
        CGContextMoveToPoint(secondaryContext, h, v);
        for( int i = 1; i < lineWidth; ++ )
        {
            SetStrokeColor(i);
            CGContextAddLineToPoint(secondaryContext, h+i, v);
            CGContextDrawPath(secondaryContext, kCGPathFillStroke);
            CGContextMoveToPoint(secondaryContext, h+i, v);
        }
    }

    //a function that sets a different color for each i
    void SetStrokeColor(int i)
    {
        CGContextSetRGBStrokeColor(secondaryContext,…
    }

上面的代码有效,但如果我在现实生活中使用它,它就是性能杀手。所以我尝试使用下面的代码来提高绘图性能。

    void drawMulticolorLine
    {
        CGContextBeginPath(secondaryContext);
        CGContextMoveToPoint(secondaryContext, h, v);
        for( int i = 1; i < lineWidth; ++ )
        {
            SetStrokeColor(i);
            CGContextAddLineToPoint(secondaryContext, h+i, v);
        }
        CGContextDrawPath(secondaryContext, kCGPathFillStroke);
    }

    //a function that sets a different color for each i
    void SetStrokeColor(int i)
    {
        CGContextSetRGBStrokeColor(secondaryContext,…
    }

如果线条颜色始终相同,则此代码具有良好的性能。

我现在的问题是,一旦调用 CGContextDrawPath,整条线就会以最后一个 CGContextSetRGBStrokeColor 的颜色绘制。

有没有办法让路径保持添加的每个线段的颜色?

任何帮助是极大的赞赏。

干杯,bzt

4

1 回答 1

0

最近在操场上玩,所以分享源代码。

extension CGFloat {
    static func random() -> CGFloat {
        return CGFloat(arc4random()) / CGFloat(UInt32.max)
    }
}

extension UIColor {
    static func random() -> UIColor {
        return UIColor(red:   .random(),
                       green: .random(),
                       blue:  .random(),
                       alpha: 1.0)
    }
}
let copy: CGPath = ...
let image = UIGraphicsImageRenderer(size: copy.boundingBoxOfPath.size).image { context in

    context.cgContext.setLineWidth(2)

    copy.applyWithBlock { element in
        switch element.pointee.type {
        case .moveToPoint:
            context.cgContext.move(to: element.pointee.points[0])
        case .addLineToPoint:
            context.cgContext.addLine(to: element.pointee.points[0])
            context.cgContext.setStrokeColor(UIColor.random().cgColor)
            context.cgContext.strokePath()
            context.cgContext.move(to: element.pointee.points[0])
        case .addQuadCurveToPoint:
            let controlPoint = element.pointee.points[0]
            let point = element.pointee.points[1]
            context.cgContext.addCurve(to: point, control1: controlPoint, control2: controlPoint)
            context.cgContext.setStrokeColor(UIColor.random().cgColor)
            context.cgContext.strokePath()
            context.cgContext.move(to: point)
        case .addCurveToPoint:
            let controlPoint1 = element.pointee.points[0]
            let controlPoint2 = element.pointee.points[1]
            let point = element.pointee.points[2]
            context.cgContext.addCurve(to: point, control1: controlPoint1, control2: controlPoint2)
            context.cgContext.setStrokeColor(UIColor.random().cgColor)
            context.cgContext.strokePath()
            context.cgContext.move(to: point)
        case .closeSubpath:
            context.cgContext.setStrokeColor(UIColor.random().cgColor)
            context.cgContext.strokePath()
        }
    }
}
于 2018-11-15T18:05:49.750 回答