How to take an image snapshot of the screen on Mac OS X Lion
Q: How do I take an image snapshot of the screen on Mac OS X Lion?
A: On Mac OS X Lion, the way to make an image snapshot from the screen is by employing Quartz Display Services.
Make sure you add the ApplicationServices framework in your Xcode project and import the ApplicationServices/ApplicationServices.h
header file.
Given a display ID (of CGDirectDisplayID
type), you can call CGDisplayCreateImage
to create a CGImage from the entire screen or CGDisplayCreateImageForRect
from a rectangular portion of it.
If you always capture from the main display, you may simply pass kCGDirectMainDisplay
to either function as shown in Listing 1. It is a shorthand for specifying the current main display.
If you might capture from a secondary display, then you can go through the list of active displays returned from CGGetActiveDisplayList
and find the one you want to capture. See Listing 2 for an example.
Listing 1 Creating an image from the entire main display
CGImageRef image = CGDisplayCreateImage(kCGDirectMainDisplay); |
Listing 2 Creating images from multiple displays
CGDisplayCount displayCount; CGDirectDisplayID displays[32]; // grab the active displays CGGetActiveDisplayList(32, displays, &displayCount); // go through the list for (int i=0; i<displayCount; i++) { // make a snapshot of the current display CGImageRef image = CGDisplayCreateImage(displays[i]); // do something with the snapshot image } |
Check out the dedicated sample code, ScreenSnapshot, for more information. It demonstrates how to use Quartz Display Services to obtain an image containing the contents of any of the connected displays and allows the user to save the image to a file on disk.
Document Revision History
Date | Notes |
---|---|
2011-08-10 | Corrected a link. Added information about the ScreenSnapshot sample. |
2011-05-13 | New document that shows how to take an image snapshot of the screen on Mac OS X Lion |
Copyright © 2011 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2011-08-10