0

I need to take a screenshot of the app. There is a button to trigger it, but I don't want the button to appear in the screenshot.

I have the following code:

- (IBAction) takePicture:(id) sender {
    button.hidden = YES;

    CGImageRef screen = UIGetScreenImage();
    UIImage* image = [UIImage imageWithCGImage:screen];
    CGImageRelease(screen);

    // Save the captured image to photo album
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    button.hidden = NO;
}

But the button still appears in the screenshot. What am I doing wrong?

egze
  • 3,200
  • 23
  • 23

2 Answers2

2

I think you capture the screen before the runloop could refresh the view (ie before it could hide the button)

you should try to change your code into something like this:

- (IBAction) takePicture:(id) sender {
    button.hidden = YES;
    [self performSelector:@selector(takeScreenShot) withObject:nil afterDelay:0.1];                    
}

- (void)takeScreenShot {
    CGImageRef screen = UIGetScreenImage();
    UIImage* image = [UIImage imageWithCGImage:screen];
    CGImageRelease(screen);

    // Save the captured image to photo album
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    button.hidden = NO;

}

this should take the screenshot after the runloop has removed your button.

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
0

You can set a timer to fire after the button is hidden, i.e.:

    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(captureScreenshot) userInfo:nil repeats:NO];

and then move the screen capturing code to your captureScreenshot method.

One more thing: UIGetScreenImage() is an undocumented method call, use of which will probably result in your application being rejected. You should instead use CALayer's renderInContext: method --see this StackOverflow post for an example.

Community
  • 1
  • 1
Vicarius
  • 304
  • 3
  • 7
  • Hi. Thanks, I'm aware about UIGetScreenImage. Just need to quickly prototype something. Not going to be a published app. – egze Feb 28 '11 at 16:25