【问题标题】:draw a multicolor line with a CGPath用 CGPath 画一条多色线
【发布时间】:2013-02-13 06:22:31
【问题描述】:

您会在下面看到每次调用 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

【问题讨论】:

  • 我不是核心图形专家,但 CGContextSetStrokePattern 对你有用吗?
  • 不,我已经考虑过了,但模式不起作用,因为 SetStrokeColor 设置的颜色是动态的。计算的每条线对于每个 i(线段)都有不同的颜色,然后每条完成的线(在 i==lineWidth 之后)都会垂直移动并绘制一条新线...

标签: ios objective-c macos core-graphics cgpath


【解决方案1】:

最近在游乐场玩,所以分享源代码。

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()
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-31
    • 1970-01-01
    相关资源
    最近更新 更多