【发布时间】: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