2016-12-14 59 views
1

我不想截取NSView的屏幕截圖,但是當我這樣做時,我得到一個沒有背景的圖像。所有子視圖都顯示,但不是背景。我用這個代碼:NSView dataWithPDF裏面rect但具有背景視圖

if let image = NSImage(data: background.dataWithPDF(inside: background.bounds)) { 
    let imageView = NSImageView(image: image) 
    imageView.imageScaling = .scaleProportionallyUpOrDown 
    return NSImageView(image: image) 
} 

我就想,好吧,當我得到的只有子視圖,然後我會讓上海華盈的屏幕截圖,我thried如下:

if let superview = background.superview, let image = NSImage(data: superview.dataWithPDF(inside: background.frame)) { 
    let imageView = NSImageView(image: image) 
    imageView.imageScaling = .scaleProportionallyUpOrDown 
    return NSImageView(image: image) 
} 

但我得到相同結果。即使我設置了我的background視圖的背景顏色,我也無法獲得沒有透明背景的圖像。

我該如何解決這個問題?

謝謝

阿圖爾

回答

0

我得到了一個答案:

background.lockFocus() 
if let rep = NSBitmapImageRep(focusedViewRect: background.bounds){ 

    let img = NSImage(size: background.bounds.size) 
    img.addRepresentation(rep) 

    return NSImageView(image: img) 
} 
background.unlockFocus() 
0

你可能已經轉換支持layer.backgroundColor到圖像的意見。然後將圖像分配到位於視圖層次索引0中的支持圖像視圖,並使用dataWithPDF - >方法成功捕獲背景。