Note that there are some explanatory texts on larger screens.

plurals
  1. POiPhone: Create a screenshot programmatically and then add it as a subview
    primarykey
    data
    text
    <p>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:</p> <pre><code> // 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]; </code></pre> <p>And this is Apple's sample code for the method:</p> <pre><code> - (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; } </code></pre> <p>Any help would be very much appreciated! Thanks.</p> <hr> <p>EDIT: This is the viewDidLoad method in which I create a TextView and then try to capture a screen shot of it:</p> <pre><code> - (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]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload