我如何获得支持NSView的图层以在剪贴板中写入PDF

时间:2019-05-28 11:54:12

标签: layer nsview nspasteboard

我正在使用带有CGImage的层支持视图,并且想使用NSView.writePDF(inside:to) API将视图图像复制到剪贴板。该视图包含我想要包含在PDF中的子视图。

我创建了一个Xcode游乐场来说明问题。

//: A Cocoa based Playground to test NSView.writePDF()

import AppKit
import PlaygroundSupport

class BaseView: NSView {

    var isSelected: Bool = false {
        didSet {
            self.needsDisplay = true
        }
    }

    var fillColor = NSColor.yellow

    var cgFillColor: CGColor {
        return self.isSelected ? fillColor.cgColor : fillColor.withAlphaComponent(0.5).cgColor
    }

    var storeLayoutCGImageRef: CGImage? {
        didSet {

            self.layer?.contents = storeLayoutCGImageRef

        }
    }

    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)
        guard let context = NSGraphicsContext.current?.cgContext else {
            return
        }

        context.setFillColor(cgFillColor)
        context.fill(dirtyRect)
    }

    override func mouseDown(with theEvent: NSEvent) {

        self.isSelected = !self.isSelected
    }

    @objc func copyImage(_ sender: Any){
         let pb = NSPasteboard.general
        pb.declareTypes([.pdf], owner: self)

        self.writePDF(inside: self.bounds, to: pb)
    }
    @objc func doNothing(_ sender: Any){

    }
    // Load image in the background
    func loadImage(completion: @escaping ()->Void){
        DispatchQueue.global().async {
            if let image = NSImage(named: "B-019969.jpg") {

                // Lets use double the size ?
                var rect = CGRect(x: 0, y: 0, width: image.size.width*3.0, height: image.size.height*3.0)
                let cgImage = image.cgImage(forProposedRect: &rect, context: nil, hints: nil)

                DispatchQueue.main.async {
                    self.storeLayoutCGImageRef = cgImage

                    completion()
                }
                return
            }

            DispatchQueue.main.async {
                //self.storeLayoutImage = nil
                self.storeLayoutCGImageRef = nil
                completion()
            }
        }
    }
}

class ModuleView: BaseView {

    let color = NSColor.purple

    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        self.fillColor = color
    }

    required init?(coder decoder: NSCoder) {
         super.init(coder: decoder)
        self.fillColor = color
    }

    override func menu(for event: NSEvent) -> NSMenu? {
        if !isSelected {
            let menu = NSMenu()
            menu.addItem(NSMenuItem(title: "Copy module", action: #selector(copyImage(_:)), keyEquivalent: ""))
            return menu
        } else {
            // Display popup menu
            let menu = NSMenu()
            menu.addItem(NSMenuItem(title: "Copy module", action: #selector(copyImage(_:)), keyEquivalent: ""))
            menu.addItem(NSMenuItem(title: "Do Something", action: #selector(doNothing(_:)), keyEquivalent: ""))
            return menu

        }
    }

}
class ShelfView: BaseView {

    let color = NSColor.yellow

    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        self.fillColor = color
        self.loadImage {
            print("Image loaded")
        }
    }
    required init?(coder decoder: NSCoder) {
        super.init(coder: decoder)
        self.fillColor = color
    }
    override func menu(for event: NSEvent) -> NSMenu? {
        if !isSelected {
            let menu = NSMenu()
            menu.addItem(NSMenuItem(title: "Copy shelf", action: #selector(copyImage(_:)), keyEquivalent: ""))
            return menu
        } else {
            // Display popup menu
            let menu = NSMenu()
            menu.addItem(NSMenuItem(title: "Copy shelf", action: #selector(copyImage(_:)), keyEquivalent: ""))
            menu.addItem(NSMenuItem(title: "Do Something", action: #selector(doNothing(_:)), keyEquivalent: ""))
            return menu

        }
    }
    override var wantsUpdateLayer: Bool {
        return true
    }
    override func updateLayer() {
        self.layer?.backgroundColor = self.cgFillColor
        self.layer?.contents = self.storeLayoutCGImageRef
    }
}

class view: BaseView
{

    var module: ModuleView?

    let color = NSColor.cyan

    override init(frame: NSRect)
    {
        super.init(frame: frame)
        self.fillColor = color
        self.addModules()
        self.addShelves()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func addModules(){

        let module = ModuleView()

        module.frame = NSRect(x: 10.0, y: 10.0, width: 180.0, height: 180.0)
        self.module = module
        self.addSubview(module)
    }
    func addShelves(){

        let shelf = ShelfView()

        shelf.frame = NSRect(x: 10.0, y: 10.0, width:160.0, height: 160.0)
        self.module?.addSubview(shelf)
    }

    override func menu(for event: NSEvent) -> NSMenu? {
        if !isSelected {
            let menu = NSMenu()
            menu.addItem(NSMenuItem(title: "Copy fixture", action: #selector(copyImage(_:)), keyEquivalent: ""))
            return menu
        } else {
            // Display popup menu
            let menu = NSMenu()
            menu.addItem(NSMenuItem(title: "Copy fixture", action: #selector(copyImage(_:)), keyEquivalent: ""))
            menu.addItem(NSMenuItem(title: "Do Something", action: #selector(doNothing(_:)), keyEquivalent: ""))
            return menu

        }
    }
}

var v = view(frame: NSRect(x: 0, y: 0, width: 200, height: 200))

PlaygroundPage.current.liveView = v

是否有办法确保该图层包含在PDF输出中?

1 个答案:

答案 0 :(得分:0)

好的,我刚刚弄清楚-基本上,您必须实现draw(_ dirtyRect)函数,该函数在打印过程中或在调用NSView.writePDF()时被调用。

请注意,在某些情况下不会调用draw()函数-例如用鼠标选择项目只会导致调用updateLayer()方法。

这是更新后的游乐场,希望这可以节省其他人的时间。

//: A Cocoa based Playground to test NSView.writePDF()

import AppKit
import PlaygroundSupport

class BaseView: NSView {

    var isSelected: Bool = false {
        didSet {
            self.needsDisplay = true
        }
    }

    var fillColor = NSColor.yellow

    var cgFillColor: CGColor {
        return self.isSelected ? fillColor.cgColor : fillColor.withAlphaComponent(0.5).cgColor
    }

    var storeLayoutCGImageRef: CGImage? {
        didSet {

            self.layer?.contents = storeLayoutCGImageRef

        }
    }

    var image: CGImage?

    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)
        guard let context = NSGraphicsContext.current?.cgContext else {
            return
        }

        context.setFillColor(cgFillColor)
        context.fill(dirtyRect)
    }

    override func mouseDown(with theEvent: NSEvent) {

        self.isSelected = !self.isSelected
    }

    @objc func copyImage(_ sender: Any){
         let pb = NSPasteboard.general
        pb.declareTypes([.pdf], owner: self)

        self.writePDF(inside: self.bounds, to: pb)
    }
    @objc func doNothing(_ sender: Any){

    }
    // Load image in the background
    func loadImage(completion: @escaping ()->Void){
        DispatchQueue.global().async {
            if let image = NSImage(named: "Sample Image.png") {

                // Lets use double the size ?
                var rect = CGRect(x: 0, y: 0, width: image.size.width*3.0, height: image.size.height*3.0)
                let cgImage = image.cgImage(forProposedRect: &rect, context: nil, hints: nil)

                DispatchQueue.main.async {
                    self.storeLayoutCGImageRef = cgImage

                    completion()
                }
                return
            }

            DispatchQueue.main.async {
                //self.storeLayoutImage = nil
                self.storeLayoutCGImageRef = nil
                completion()
            }
        }
    }
}

class ModuleView: BaseView {

    let color = NSColor.purple

    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        self.fillColor = color
    }

    required init?(coder decoder: NSCoder) {
         super.init(coder: decoder)
        self.fillColor = color
    }

    override func menu(for event: NSEvent) -> NSMenu? {
        if !isSelected {
            let menu = NSMenu()
            menu.addItem(NSMenuItem(title: "Copy module", action: #selector(copyImage(_:)), keyEquivalent: ""))
            return menu
        } else {
            // Display popup menu
            let menu = NSMenu()
            menu.addItem(NSMenuItem(title: "Copy module", action: #selector(copyImage(_:)), keyEquivalent: ""))
            menu.addItem(NSMenuItem(title: "Do Something", action: #selector(doNothing(_:)), keyEquivalent: ""))
            return menu

        }
    }

}
class ShelfView: BaseView {

    let color = NSColor.green

    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        self.fillColor = color
        self.loadImage {
            print("Image loaded")
        }
    }
    required init?(coder decoder: NSCoder) {
        super.init(coder: decoder)
        self.fillColor = color
    }
    override func menu(for event: NSEvent) -> NSMenu? {
        if !isSelected {
            let menu = NSMenu()
            menu.addItem(NSMenuItem(title: "Copy shelf", action: #selector(copyImage(_:)), keyEquivalent: ""))
            return menu
        } else {
            // Display popup menu
            let menu = NSMenu()
            menu.addItem(NSMenuItem(title: "Copy shelf", action: #selector(copyImage(_:)), keyEquivalent: ""))
            menu.addItem(NSMenuItem(title: "Do Something", action: #selector(doNothing(_:)), keyEquivalent: ""))
            return menu

        }
    }

    override func draw(_ dirtyRect: NSRect) {
        print("ShelfView.draw() called")
        super.draw(dirtyRect)
        guard let context = NSGraphicsContext.current?.cgContext else {
            return
        }
        if let image = self.storeLayoutCGImageRef {
            context.draw(image, in: self.bounds)
        }
    }

    override var wantsUpdateLayer: Bool {
        return true
    }
    override func updateLayer() {
        print("updateLayer() called")
        self.layer?.backgroundColor = self.cgFillColor
        self.layer?.contents = self.storeLayoutCGImageRef

    }
}

class view: BaseView
{

    var module: ModuleView?

    let color = NSColor.cyan

    override init(frame: NSRect)
    {
        super.init(frame: frame)
        self.fillColor = color
        self.addModules()
        self.addShelves()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func addModules(){

        let module = ModuleView()

        module.frame = NSRect(x: 10.0, y: 10.0, width: 180.0, height: 180.0)
        self.module = module
        self.addSubview(module)
    }
    func addShelves(){

        let shelf = ShelfView()

        shelf.frame = NSRect(x: 10.0, y: 10.0, width:160.0, height: 160.0)
        self.module?.addSubview(shelf)
    }

    override func menu(for event: NSEvent) -> NSMenu? {
        if !isSelected {
            let menu = NSMenu()
            menu.addItem(NSMenuItem(title: "Copy fixture", action: #selector(copyImage(_:)), keyEquivalent: ""))
            return menu
        } else {
            // Display popup menu
            let menu = NSMenu()
            menu.addItem(NSMenuItem(title: "Copy fixture", action: #selector(copyImage(_:)), keyEquivalent: ""))
            menu.addItem(NSMenuItem(title: "Do Something", action: #selector(doNothing(_:)), keyEquivalent: ""))
            return menu

        }
    }
}

var v = view(frame: NSRect(x: 0, y: 0, width: 200, height: 200))

PlaygroundPage.current.liveView = v