How do I take a “screenshot” of an NSView?

前端 未结 4 1626
说谎
说谎 2020-12-05 05:36

I need to take the contents of an NSView and put them in an NSImage, for an experimental project. Is this possible? I did some Googling, tried two

相关标签:
4条回答
  • 2020-12-05 06:14
    let dataOfView = view.dataWithPDFInsideRect(view.bounds)
    let imageOfView = NSImage(data: dataOfView)
    
    0 讨论(0)
  • 2020-12-05 06:21

    I thinkt, Debashis is thinking more of things like http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CocoaDrawingGuide/Images/Images.html#//apple_ref/doc/uid/TP40003290-CH208-BCIIIJFB

    Greetings

    0 讨论(0)
  • 2020-12-05 06:31
    [[NSImage alloc] initWithData:[view dataWithPDFInsideRect:[view bounds]]];
    
    0 讨论(0)
  • 2020-12-05 06:37

    From WWDC 2012 Session 245 (translated to Swift):

    let viewToCapture = self.window!.contentView!
    let rep = viewToCapture.bitmapImageRepForCachingDisplay(in: viewToCapture.bounds)!
    viewToCapture.cacheDisplay(in: viewToCapture.bounds, to: rep)
    
    let img = NSImage(size: viewToCapture.bounds.size)
    img.addRepresentation(rep)
    
    0 讨论(0)
提交回复
热议问题