swift - 点不会在 swift MacOS 中绘制在 CGContext 中

标签 swift macos core-graphics

我有一个问题。我的点 (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()!

}

谁能帮帮我?

最佳答案

您定义了一条贝塞尔曲线路径,但您从不要求绘制它。

添加:

context.fillPath() 

或者:

context.drawPath(using: .fill)

在循环之后。

关于swift - 点不会在 swift MacOS 中绘制在 CGContext 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50600260/

相关文章:

swift - 使用 for 循环显示 4 个纹理

ios - 如何在 iOS 项目中使用 HDR 颜色?

ios - 更改 UIControlState.Highlighted 上的 UIButton backgroundColor?

xcode - 在 xcode 7 中使用 Mac App 预配置菜单项

xcode - applicationDidFinishLaunching 未调用

ios - 倾斜文本后创建 UIImage

ios - 从 XIB 加载 View 到 ViewController 和 VIewCell

macos - 有没有类似 Mac OS X 的 Winsplit Revolution 之类的东西?

core-graphics - 如何从 ALAssetRepresentation 创建有效的 CGImageSourceRef?

macos - 绘制具有渐变颜色的字符串