【问题标题】:Dots won't draw in CGContext in swift MacOS在 Swift MacOS 中,点不会在 CGContext 中绘制
【发布时间】:2018-05-30 08:47:54
【问题描述】:

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

}

谁能帮帮我?

【问题讨论】:

    标签: swift macos core-graphics


    【解决方案1】:

    您定义了一条贝塞尔路径,但从未请求绘制它。

    添加:

    context.fillPath() 
    

    或者:

    context.drawPath(using: .fill)
    

    在循环之后。

    【讨论】:

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