2016-08-22 24 views
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 
    } 
} 
+0

是因爲合成模式是NSCompositeCopy? NSRectFillUsingOperation會更好嗎? – matt

+0

是的,你是完全正確的。 NSRectFillUsingOperation + CompositeSourceOver的行爲與NSBezierPath + fill相同,NSRectFillUsingOperation + CompositeCopy與NSRectFill相同。嘗試寫它作爲答案,我給你信貸。謝謝。 –

+0

謝謝,這樣做。很高興這成功了! – matt

回答

3

我大多的事情了AppKit側的iOS程序員,而不是很流利的這些日子結束了,但我的猜測是,你得到了錯誤的NSCompositingOperation。我從文檔中看到NSRectFill使用NSCompositeCopy。如果你使用NSRectFillUsingOperation,那麼你可以指定合成操作。