Every UIWindow (inherited from UIView) and UIView is backed by a CALayer. The CALayer/-renderInContext: method lets you render a layer and its sublayers to a graphics context. So, to grab a snapshot of the entire screen, you can iterate through each window on the screen and render its layer hierarchy to a destination context. Once finished you can get the screenshot image via the UIGraphicsGetImageFromCurrentImageContext function, as shown below.

WARNING: CALayer/-renderInContext: does not capture OpenGL ES or video content. It captures only your UIKit and Quartz drawing.

Note: Starting from iOS 4, UIGraphicsBeginImageContextWithOptions allows you to provide with a scale factor. A scale factor of zero sets it to the scale factor of the device’s main screen. This enables you to get the sharpest, highest-resolustion snapshot of the display, including a Retina Display.

Listing 1: Get a screenshot image

Leave a Reply

电子邮件地址不会被公开。 必填项已用*标注