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 CGImageR开发者_如何转开发ef.
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.
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. ;-)
精彩评论