0

I am writing a sreen cut APP on mac OS X, i can only get the visible part of window like this:

CGImageRef imageRef = CGWindowListCreateImage(CGRectNull,
                                              kCGWindowListOptionIncludingWindow,
                                              self.windowID,
                                              kCGWindowImageBoundsIgnoreFraming);
_windowImage = [[NSImage alloc] initWithCGImage:imageRef size:self.windowFrame.size];
CGImageRelease(imageRef);

But when the window can scroll, i want to get the whole part not only the visible part, does anyone have some advices? Thanks!

melody5417
  • 355
  • 2
  • 13

1 Answers1

0
//capture 获取scrollview的截图
NSRect bounds = NSMakeRect(0, 0,  [self.scrollView.documentView     frame].size.width,[self.scrollView.documentView frame].size.height);

//  //只能获取可见部分 only can grap the visible part of the scrollview
//  NSBitmapImageRep* captureImage = [(NSView      *)self.scrollView.documentView     bitmapImageRepForCachingDisplayInRect:bounds];
//  [self.scrollView cacheDisplayInRect:bounds toBitmapImageRep:captureImage];
//  NSImage *image = [[NSImage alloc] initWithCGImage:[captureImage CGImage] size:captureImage.size];

//can grab the whole part
[self.scrollView lockFocus];
NSImage *image = [[NSImage alloc] initWithData:[(NSView *)self.scrollView.documentView
                                                  dataWithPDFInsideRect:bounds]];
[self.scrollView unlockFocus];
melody5417
  • 355
  • 2
  • 13