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 の不透明度によるものですか?

別のView Controllerが表示されたときに画面の画像を取得する方法を知りたいです。

デフォルトのコントローラのスクリーンショットが取得できないということですか?

助けてください。

前もって感謝します。

4

1 に答える 1

0

まあ、あなたは単に追加することができます

ViewOnTop.alpha = 0.0;

スクリーンショットを撮りたいビューの上にあるビューに移動します。これにより、最初に上部のビューがアルファプロパティを0に減らし、実質的に透明になり、スクリーンショットを撮っています。

ここに上記のコードを追加します

- (UIImage*)screenshot {

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

編集 :

同じコードで、表示されているビューにプロパティをviewcontroller追加してdismissViewController、最終的にスクリーンショットが必要なビューが表示されるようにします。

于 2013-08-26T11:41:55.057 に答える