現在の画面のスクリーンショットを取得したい。
ただし、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 の不透明度によるものですか?
別のView Controllerが表示されたときに画面の画像を取得する方法を知りたいです。
デフォルトのコントローラのスクリーンショットが取得できないということですか?
助けてください。
前もって感謝します。