Hide UIButton before taking a screenshot
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?
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.
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.
精彩评论