Creating a Dashboard Screenshot

Operating system requirements: iOS 5.0 or later.

Mobile device: iPad.

Description

This example displays creating a dashboard screenshot which is then saved into the folder with dashboard resources.

Source Code

Executing the example requires to place the following code in the body of the executeExample method of the ViewController class (see the Displaying of Dashboard section):

// Create a current application window screenshot
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
    UIGraphicsBeginImageContextWithOptions(keyWindow.bounds.size, YES, [UIScreen mainScreen].scale);
    } else {
    UIGraphicsBeginImageContext(keyWindow.bounds.size);
}
[keyWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
// Get image
UIImage *image1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *screenshot = UIImagePNGRepresentation(image1);
// Get image data
SNData data = NData::dataWithBytes([screenshot bytes], [screenshot length]);
// Create a regular report copy
SPPLDashboardReport copyReport = m_dashboardReport->copy()->qClass<PPLDashboardReport>();
// Set custom data for report copy
copyReport->setData(data);
// Then save screenshot to hard drive
NSData *screenshotData = (NSData *)copyReport->data()->nsObject();
UIImage *image = [UIImage imageWithData:screenshotData];
// Get first application resource
SPPLReportResource resource = copyReport->resources()->items()->objectAtIndex<PPLReportResource>(0);
// Determine full path to this resource
NSMutableString *filePath = (NSMutableString *)resource->fullPath()->nsString();
int location = [filePath rangeOfString:@"/" options:NSBackwardsSearch].location;
NSMutableString *path = [(NSMutableString *)[filePath substringToIndex:location] mutableCopy];
// Determine image path
[path appendString:@"/"];
[path appendString:@"Image.png"];
// Save image by specified path
[UIImagePNGRepresentation(image) writeToFile:path atomically:YES];
NSLog(@"Full path to saved dashboard screenshot: %@", path);

After executing the example a dashboard screenshot is created that is then saved into the folder with dashboard resources. The path to this file is displayed in the development environment console:

Full path to saved dashboard screenshot: /Users/sea/Library/Application Support/iPhone Simulator/7.0.3/Applications/660138DE-60E2-4BC5-9263-E4D588C7DD6C/Documents/repositories/C6D1F3B71B945F5A5D708E56526E15AF/DASHBOARD_MOB6/Image.png

See also:

Example of pplib++ Library Use