+ return saved_screenshot;
+}
+
+- (void) saveScreenshot
+{
+ // Most of this is from:
+ // http://developer.apple.com/library/ios/#qa/qa1703/_index.html
+ // The rotation stuff is by me.
+
+ CGSize size = [[UIScreen mainScreen] bounds].size;
+
+ UIInterfaceOrientation orient =
+ [[window rootViewController] interfaceOrientation];
+ if (orient == UIInterfaceOrientationLandscapeLeft ||
+ orient == UIInterfaceOrientationLandscapeRight) {
+ // Rotate the shape of the canvas 90 degrees.
+ double s = size.width;
+ size.width = size.height;
+ size.height = s;
+ }
+
+
+ // Create a graphics context with the target size
+ // On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to
+ // take the scale into consideration
+ // On iOS prior to 4, fall back to use UIGraphicsBeginImageContext
+
+ if (UIGraphicsBeginImageContextWithOptions)
+ UIGraphicsBeginImageContextWithOptions (size, NO, 0);
+ else
+ UIGraphicsBeginImageContext (size);
+
+ CGContextRef ctx = UIGraphicsGetCurrentContext();
+
+
+ // Rotate the graphics context to match current hardware rotation.
+ //
+ switch (orient) {
+ case UIInterfaceOrientationPortraitUpsideDown:
+ CGContextTranslateCTM (ctx, [window center].x, [window center].y);
+ CGContextRotateCTM (ctx, M_PI);
+ CGContextTranslateCTM (ctx, -[window center].x, -[window center].y);
+ break;
+ case UIInterfaceOrientationLandscapeLeft:
+ case UIInterfaceOrientationLandscapeRight:
+ CGContextTranslateCTM (ctx,
+ ([window frame].size.height -
+ [window frame].size.width) / 2,
+ ([window frame].size.width -
+ [window frame].size.height) / 2);
+ CGContextTranslateCTM (ctx, [window center].x, [window center].y);
+ CGContextRotateCTM (ctx,
+ (orient == UIInterfaceOrientationLandscapeLeft
+ ? M_PI/2
+ : -M_PI/2));
+ CGContextTranslateCTM (ctx, -[window center].x, -[window center].y);
+ break;
+ default:
+ break;
+ }
+
+ // Iterate over every window from back to front
+ //
+ for (UIWindow *win in [[UIApplication sharedApplication] windows]) {
+ if (![win respondsToSelector:@selector(screen)] ||
+ [win screen] == [UIScreen mainScreen]) {
+
+ // -renderInContext: renders in the coordinate space of the layer,
+ // so we must first apply the layer's geometry to the graphics context
+ CGContextSaveGState (ctx);
+
+ // Center the context around the window's anchor point
+ CGContextTranslateCTM (ctx, [win center].x, [win center].y);
+
+ // Apply the window's transform about the anchor point
+ CGContextConcatCTM (ctx, [win transform]);
+
+ // Offset by the portion of the bounds left of and above anchor point
+ CGContextTranslateCTM (ctx,
+ -[win bounds].size.width * [[win layer] anchorPoint].x,
+ -[win bounds].size.height * [[win layer] anchorPoint].y);
+
+ // Render the layer hierarchy to the current context
+ [[win layer] renderInContext:ctx];
+
+ // Restore the context
+ CGContextRestoreGState (ctx);
+ }
+ }
+
+ if (saved_screenshot)
+ [saved_screenshot release];
+ saved_screenshot = [UIGraphicsGetImageFromCurrentImageContext() retain];
+
+ UIGraphicsEndImageContext();
+}
+
+
+- (void) openPreferences: (NSString *) saver
+{
+ [self loadSaver:saver launch:NO];
+ if (! saverView) return;
+
+ NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
+ [prefs setObject:saver forKey:@"selectedSaverName"];
+ [prefs synchronize];
+
+ [rootViewController pushViewController: [saverView configureView]
+ animated:YES];
+}
+
+
+#endif // USE_IPHONE