我知道我可以使用NSRectFill(bounds)填充矩形.但是我想保留PDF输出的透明度,我发现只能使用NSBezierPath(rect:bounds).fill() 这两者有什么不同(幕后)? func drawBackground() { CGContextSaveGState(currentCo
这两者有什么不同(幕后)?
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
}
}
我大部分时间都是iOS程序员,而且在AppKit方面这些日子并不是很流畅,但我的猜测是你得到了错误的NSCompositingOperation.我从文档中看到NSRectFill使用NSCompositeCopy.如果您使用NSRectFillUsingOperation,可能会更好地工作,您可以在其中指定合成操作.
