n.evermind
n.evermind

Reputation: 12004

iPhone: Create a screenshot programmatically and then add it as a subview

I would like to call a method which takes a screenshot and then load this screenshot as a subview. I am using Apples sample code on how to take a screen shot (see below) and was trying to use the result (an image) in my code. However, I don't really know how to get the image from the method into my code. This is what I tried; it's obviously wrong, but it's all I could come up with:

    // Test Screenshot:

screenShot = [UIImage screenshot]; // THIS DOESN'T WORK
screenShotView = [[UIImageView alloc] initWithImage:screenShot];
[screenShotView setFrame:CGRectMake(0, 0, 320, 480)];
[self.view addSubview:screenShotView];

And this is Apple's sample code for the method:

 - (UIImage*)screenshot 
{
    NSLog(@"Shot");

    // Create a graphics context with the target size
    // On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration
    // On iOS prior to 4, fall back to use UIGraphicsBeginImageContext
    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])
        {
            // -renderInContext: renders in the coordinate space of the layer,
            // so we must first apply the layer's geometry to the graphics context
            CGContextSaveGState(context);
            // Center the context around the window's anchor point
            CGContextTranslateCTM(context, [window center].x, [window center].y);
            // Apply the window's transform about the anchor point
            CGContextConcatCTM(context, [window transform]);
            // Offset by the portion of the bounds left of and above the anchor point
            CGContextTranslateCTM(context,
                                  -[window bounds].size.width * [[window layer] anchorPoint].x,
                                  -[window bounds].size.height * [[window layer] anchorPoint].y);

            // Render the layer hierarchy to the current context
            [[window layer] renderInContext:context];

            // Restore the context
            CGContextRestoreGState(context);
        }
    }

    // Retrieve the screenshot image
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return image;
}

Any help would be very much appreciated! Thanks.


EDIT: This is the viewDidLoad method in which I create a TextView and then try to capture a screen shot of it:

    - (void)viewDidLoad {


    // Setup TextView:

    NSString* someText = @"Some Text";
    CGRect frameText = CGRectMake(0, 0, 320, 480); 
    aTextView = [[UITextView alloc] initWithFrame:frameText];
    aTextView.text = someText;  
[self.view addSubview:aTextView];


    // Test Screenshot:

    screenShotView = [[UIImageView alloc] initWithImage:[self screenshot]];
    [screenShotView setFrame:CGRectMake(10, 10, 200, 200)];
    [self.view addSubview:screenShotView];

    [self.view bringSubviewToFront:screenShotView];

    [super viewDidLoad];



}

Upvotes: 3

Views: 6624

Answers (1)

Cyprian
Cyprian

Reputation: 9453

To use the image just change this line

screenShot = [UIImage screenshot];

To

screenShot = [self screenshot];

Edit: Check to see if the [self screenshot] returns a valid image or nil.

   - (void)viewDidLoad {


    // Setup TextView:

    NSString* someText = @"Some Text";
    CGRect frameText = CGRectMake(0, 0, 320, 480); 
    aTextView = [[UITextView alloc] initWithFrame:frameText];
    aTextView.text = someText;  
    [self.view addSubview:aTextView];


    // Test Screenshot:

    UIImage *screenShotImage = [self screenShot];
    if(screenShot){
            screenShotView = [[UIImageView alloc] initWithImage:screenShotImage];
            [screenShotView setFrame:CGRectMake(10, 10, 200, 200)];
            [self.view addSubview:screenShotView];

            [self.view bringSubviewToFront:screenShotView];
    }else
         NSLog(@"Something went wrong in screenShot method, the image is nil");

    [super viewDidLoad];



}

Upvotes: 3

Related Questions