Majid Laissi
Majid Laissi

Reputation: 19788

Take screenshot of current foreground application

I'm using this code to take a screenshot in iOS:

- (UIImage*)screenshot 
{
    CGSize imageSize = [[UIScreen mainScreen] bounds].size;
    if (NULL != UIGraphicsBeginImageContextWithOptions)
        UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
    else
        UIGraphicsBeginImageContext(imageSize);

    CGContextRef context = UIGraphicsGetCurrentContext();

    // Iterate over every window from back to front
    for (UIWindow *window in [[UIApplication sharedApplication] windows]) 
    {
        if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen])
        {
            CGContextSaveGState(context);
            CGContextTranslateCTM(context, [window center].x, [window center].y);
            CGContextConcatCTM(context, [window transform]);
            CGContextTranslateCTM(context,
                  -[window bounds].size.width * [[window layer] anchorPoint].x,
                  -[window bounds].size.height * [[window layer] anchorPoint].y);

            [[window layer] renderInContext:context];

            CGContextRestoreGState(context);
        }
    }

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return image;
}

The code is run when my application is in the background.

The problem is that it always takes a screenshot of my application, and instead I would like it to take the current screen.

Is it possible? how?

-- EDIT --

Actually it is possible, (see this appstore application for example). So the remaining question is how?

Upvotes: 0

Views: 434

Answers (3)

user1040049
user1040049

Reputation:

This is not possible due to sandboxing of apps. Apps are not supposed to obtain the state of other third-party apps.

Upvotes: 3

A'sa Dickens
A'sa Dickens

Reputation: 2085

if by entering the back ground you are referring to when a user minimizes the app, then no it is impossible for the fact apple would not allow you to make an application that takes a picture of the user's device from out side your app

but i may be misunderstanding your question, if you post 2 screen shots of the before and after you transition from foreground to background so i can see where you want to take the screen shot of, i can give you a better response :D

Upvotes: 1

Undo
Undo

Reputation: 25697

No.

There's a reason the code always takes a screenshot of your application: it's your application.

Imagine the privacy implications of a backgrounded app being able to take screenshots of the current screen:

I, the user, have just hit the home button while using Bob's Malicious Money Stealer, and have switched over to look at my email - which happens to be a bill from my bank, which happens to include my account number, ATM card number, and my PIN (it's not a very good bank).

Now, the malicious app I was just using decides now would be an opportune time to see what I'm up to. It takes a screenshot of my current screen, and uploads it to a server.

The next day, all my money is gone, and I sue Apple.

Upvotes: 7

Related Questions