我想获取当前屏幕的屏幕截图。
但是每当 MFMailComposeViewController 或任何其他视图控制器出现时,绘制的屏幕截图都不会出现黑屏。
//Code to draw what ever is present currently on the screen
- (UIImage*)screenshot {
CGSize imageSize = [[UIScreen mainScreen] bounds].size;
if (NULL != UIGraphicsBeginImageContextWithOptions)
UIGraphicsBeginImageContextWithOptions(imageSize,YES, 0.0);
else
UIGraphicsBeginImageContext(imageSize);
CGContextRef context = UIGraphicsGetCurrentContext();
// Iterate over every window from back to front
for (UIWindow *currentWindow in [[UIApplication sharedApplication] windows])
{
if (![currentWindow respondsToSelector:@selector(screen)] || [currentWindow screen] == [UIScreen mainScreen])
{
CGContextSaveGState(context);
NSLog(@"current alpha %f", currentWindow.alpha);
CGContextSetAlpha(context, currentWindow.alpha);
// Center the context around the window's anchor point
CGContextTranslateCTM(context, [currentWindow center].x, [currentWindow center].y );
// Apply the window's transform about the anchor point
CGContextConcatCTM(context, [currentWindow transform]);
// Offset by the portion of the bounds left of and above the anchor point
CGContextTranslateCTM(context,
-[currentWindow bounds].size.width * [[currentWindow layer] anchorPoint].x,
-([currentWindow bounds].size.height ) * [[currentWindow layer] anchorPoint].y );
// Render the layer hierarchy to the current context
[[currentWindow layer] renderInContext:context];
// Restore the context
CGContextRestoreGState(context);
}
}
// Retrieve the screenshot image
UIImage *image = UIGraphicsGetImageFromCurrentImageContext().retain;
UIGraphicsEndImageContext();
return [image autorelease];}
当视图控制器出现时,上述代码中的 for 循环迭代不止一次,当 mfmailcomposeviewcontroller 出现时,我得到一个空白图像。尽管下面的视图控制器的标签栏控制器是可见的。
但是当我按下邮件编辑器的取消按钮时,屏幕上出现的操作表也不会被绘制。
这是由于 UIGraphicsBeginImageContextWithOptions 中的不透明度造成的吗?
我想知道当另一个视图控制器出现时如何获取屏幕图像。
是不是无法获取默认控制器截图?
请帮忙。
提前致谢。