4
我知道我可以使用NSRectFill(bounds)填充矩形。然而,我想保留PDF輸出的透明度,我發現我只能用NSBezierPath(rect:bounds).fill() 這樣做,那兩者有什麼區別(幕後)?NSRectFill和NSBezierPath(rect)有什麼區別.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會更好嗎? – matt
是的,你是完全正確的。 NSRectFillUsingOperation + CompositeSourceOver的行爲與NSBezierPath + fill相同,NSRectFillUsingOperation + CompositeCopy與NSRectFill相同。嘗試寫它作爲答案,我給你信貸。謝謝。 –
謝謝,這樣做。很高興這成功了! – matt