tags:

views:

32

answers:

1

I have a specific function on an app that is intended to work on iPhone 4 and iPad. I don't have an iPad yet (because Apple forgot my country), so I have to test it on simulator.

I have a method that needs to grab the contents of the main screen as a CGImageRef.

I have this method:

- (CGImageRef)takeScreenshot {
    UIWindow *theScreen = [[UIApplication sharedApplication].windows objectAtIndex:0];
    UIGraphicsBeginImageContext(theScreen.frame.size);
    [[theScreen layer] renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return screenshot.CGImage;
}

When I compile and test the iPhone version of my app for the iPhone, it works perfectly. But when I compile and test the iPad version on iPad simulator, it returns a black screen.

For it to work, I have to change the "objectAtIndex:0" to "objectAtIndex:1" on the second line.

Is there any logic for this or it is just a simulator bug? As far as I know, index 0 is always the main screen.

Is there any other way to capture the main screen content from another class?

thanks in advance.

+1  A: 

Maybe use the keyWindow to get the correct window directly?

UIWindow *theScreen = [[UIApplication sharedApplication] keyWindow];

For the country problem - consider ordering one from another country. ;-)

Eiko
THANKS, the code works now!!!!!! Can you please tell me what is the role of this keyWindow property? Will the keyWindow be always the mainScreen? I am afraid of using this without knowing the implications or the user will get black screens. Regarding to the country problem, Apple will not sell and from another country and forbids any dealer from doing this. My only option is ebay. Too risky and I will have a device without warranty (Apple just covers warranties on the original country the device was bought).
Digital Robot
keyWindow is the screen that is currently actively shown. I'd think that fiddling with the array of windows is too deep into the framework and imposes (invalid) assumptions. Typically, there is one call to makeKeyAndVisible somewhere in your startup setting this.I totally understand the problem with warrenty etc, having bought mine on ebay before the official release in Germany at a premium...
Eiko
thanks again!!!
Digital Robot