Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

screen capture using objective c for mac

Does anyone has any idea how can I capture screen using objective c in mac os? to be more specific, how can I capture the active / focused application screen then create an image into a specified path.
Any help is highly appreciated.

like image 851
Daniel Avatar asked Nov 06 '09 06:11

Daniel


2 Answers

@Daniel, You don't need to understand and implement the whole "Son of Grab". You just need the code below.

The following function will give you the screenshot

// This just invokes the API as you would if you wanted to grab a screen shot. The equivalent using the UI would be to
// enable all windows, turn off "Fit Image Tightly", and then select all windows in the list.
CGImageRef screenShot = CGWindowListCreateImage(CGRectInfinite, kCGWindowListOptionOnScreenOnly, kCGNullWindowID, kCGWindowImageDefault);

Use the following code to convert it to a NSImage

NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithCGImage:screenShot];
// Create an NSImage and add the bitmap rep to it...
NSImage *image = [[NSImage alloc] init];
[image addRepresentation:bitmapRep];
[bitmapRep release];
bitmapRep = nil;
like image 173
ThE uSeFuL Avatar answered Sep 16 '22 15:09

ThE uSeFuL


Have you checked out Apple's “Son of Grab” for capturing images of windows with the CGWindow api?

like image 31
Arkku Avatar answered Sep 16 '22 15:09

Arkku