NSRectFill和NSBezierPath(rect).fill之间有什么区别

时间:2016-08-22 16:13:05

标签: swift cocoa transparency cgcontext nsbezierpath

我知道我可以使用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
    }
}

1 个答案:

答案 0 :(得分:3)

我主要是一名iOS程序员,而且这些日子在AppKit方面并不是很流畅,但我的猜测是你得到了错误的NSCompositingOperation。我从文档中看到NSRectFill使用了NSCompositeCopy。如果您使用NSRectFillUsingOperation,可能会更好地工作,您可以在其中指定合成操作。

相关问题