首页 文章

Dots不会在swift MacOS中绘制CGContext

提问于
浏览
0

我有一个问题 . 我的点(GCRect)不会在我的CGImage上绘制 . 坐标是正确的 .

这是我的代码

public func drawFaceDots (onImage image : CGImage) -> CGImage {
    let faceRect = CGRect(x: 0, y: 0, width: image.width, height: image.height)
    let diameter : CGFloat = 5

    print(faceRect)
    let context = CGContext(data:  nil, width: image.width, height: image.height, bitsPerComponent: 8, bytesPerRow: 4 * image.width, space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: CGImageAlphaInfo.premultipliedFirst.rawValue)!

    //context.draw(image, in: faceRect)
    context.setFillColor(NSColor.red.cgColor)

    for point in self.orignalFace! {
        print("Point(\(point.x);\(point.y))")
        let widthX = point.x * CGFloat(image.width) + diameter
        let heightY = point.y * CGFloat(image.height) + diameter
        let boundingRect = CGRect(x: CGFloat(image.width) * point.x, y: CGFloat(image.height) * point.y, width: widthX, height: heightY)

        print("BoundingRect : \(boundingRect))")

        context.addEllipse(in: boundingRect)
        context.setFillColor(NSColor.green.cgColor)

    }

    return context.makeImage()!

}

谁能帮我?

1 回答

  • 1

    您定义了一个贝塞尔曲线路径,但您从未请求绘制它 .

    加:

    context.fillPath()
    

    或者:

    context.drawPath(using: .fill)
    

    循环之后 .

相关问题