【问题标题】:What is the difference between NSRectFill and NSBezierPath(rect).fillNSRectFill 和 NSBezierPath(rect).fill 有什么区别
【发布时间】:2016-08-22 16:13:04
【问题描述】:

我知道我可以使用 NSRectFill(bounds) 填充一个矩形。但是我想保留 PDF 输出的透明度,我发现我只能使用 NSBezierPath(rect: bounds).fill() 这两者有什么区别(在幕后)?

func drawBackground() {
    CGContextSaveGState(currentContext)
    if (NSGraphicsContext.currentContextDrawingToScreen()) {
        NSColor(patternImage: checkerboardImage).set()
        NSRectFillUsingOperation(bounds, NSCompositingOperation.CompositeSourceOver)
    }
    NSColor.clearColor().setFill()
    //NSRectFill(bounds) //option 1
    NSBezierPath(rect: bounds).fill() // option 2
    CGContextRestoreGState(currentContext)
}

extension NSImage {

    static func checkerboardImageWithSize(size : CGFloat) -> NSImage {
        let fullRect = NSRect(x: 0, y: 0, width: size, height: size)
        let halfSize : CGFloat = size * 0.5;
        let upperSquareRect = NSRect(x: 0, y: 0, width: halfSize, height: halfSize);
        let bottomSquareRect = NSRect(x: halfSize, y: halfSize, width:halfSize, height: halfSize);
        let image = NSImage(size: NSSize(width: size, height: size))
        image.lockFocus()
        NSColor.whiteColor()
        NSRectFill(fullRect)
        NSColor(deviceWhite: 0.0, alpha:0.1).set()
        NSRectFill(upperSquareRect)
        NSRectFill(bottomSquareRect)
        image.unlockFocus()
        return image
    }
}

【问题讨论】:

  • 是不是因为合成模式是NSCompositeCopy? NSRectFillUsingOperation 会更好吗?
  • 是的,你完全正确。 NSRectFillUsingOperation+CompositeCopy 的作用与 NSRectFill 相同,而 NSRectFillUsingOperation+CompositeSourceOver 的行为类似于 NSBezierPath + 填充。试着把它写成答案,我给你信用。谢谢。
  • 谢谢,这样做了。很高兴这成功了!

标签: swift cocoa transparency cgcontext nsbezierpath


【解决方案1】:

我主要是一名 iOS 程序员,这些天在 AppKit 方面不是很流利,但我猜你弄错了 NSCompositingOperation。我从文档中看到 NSRectFill 使用 NSCompositeCopy。如果您使用NSRectFillUsingOperation 可能会更好,您可以在其中指定合成操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-25
    • 2014-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-02
    • 2011-12-12
    • 2010-09-16
    相关资源
    最近更新 更多