2

我想获取当前屏幕的屏幕截图。

但是每当 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 中的不透明度造成的吗?

我想知道当另一个视图控制器出现时如何获取屏幕图像。

是不是无法获取默认控制器截图?

请帮忙。

提前致谢。

4

1 回答 1

0

那么你可以简单地添加

ViewOnTop.alpha = 0.0;

到您要截屏的视图上方的视图。这将首先使顶部的 View将其alpha属性降低为0,从而使其几乎透明,然后您将截取屏幕截图

在此处添加上述代码

- (UIImage*)screenshot {

ViewOnTop.alpha = 0.0;
............
...................
}

编辑 :

对于viewcontroller在同一代码中,将dismissViewController属性添加到正在呈现的视图中,以便最终可以看到您想要截屏的视图。

于 2013-08-26T11:41:55.057 回答