This code fragment will return a CGImageRef that contains everything shown on the desktop for a given rectangle. It requires the ApplicationServices framework. The screen coordinates are flipped and the origin is at the top-left corner of the screen. In this case, the image ref would be owned by the caller and would need to be released with CGImageRelease
when the caller was finished with it.
#import <ApplicationServices/ApplicationServices.h>
CGImageRef createScreenCapture(CGRect rect) {
CGImageRef image = CGWindowCreateImage(
rect,
kCGWindowListOptionOnScreenOnly,
0,
kCGWindowImageDefault);
return image;
}