SpriteKit:为什么我不能隐藏SKSprite节点并在此之后立即截屏?

时间:2014-12-13 07:33:48

标签: ios camera sprite-kit screenshot sknode

这可能是一个愚蠢的问题,但为什么当我执行隐藏按钮时,屏幕截图是在隐藏按钮之前发生的?有没有办法确保在截图发生之前按钮消失了?我需要单独的线程调用吗?谢谢!

if([node.name isEqualToString:@"ScreenshotButton"])
{
    [self UserHideAllButtons];   //This hides all menus and buttons
    [self captureFullScreen];  This takes a screenshot and sends it to camera 
    roll
}

-(void)UserHideAllButtons
{
    [self showItem:-1 withItemNamed:btnCandy];
    [self showItem:-1 withItemNamed:btnCharacter];
    [self showItem:-1 withItemNamed:btnDecor];
    [self showItem:-1 withItemNamed:btnGifts];
    [self showItem:-1 withItemNamed:btnGreeting];

    [self showItem:-1 withItemNamed:btnCandyPressed];
    [self showItem:-1 withItemNamed:btnCharacter];
    [self showItem:-1 withItemNamed:btnDecorPressed];
    [self showItem:-1 withItemNamed:btnGiftsPressed];
    [self showItem:-1 withItemNamed:btnGreetingPressed];



    [self hideObjects:-1 withArrayNamed:candyObjects];
    [self hideObjects:-1 withArrayNamed:characterObjects];
    [self hideObjects:-1 withArrayNamed:decorObjects];
    [self hideObjects:-1 withArrayNamed:giftsObjects];
    [self hideObjects:-1 withArrayNamed:greetingObjects];

    [self clearMenu];
    sliderArrowBtn.zPosition=-1;
    sliderMenu.zPosition=-1;
    slider.zPosition=-1;
    selectedItemBack.zPosition=-1;
}

-(UIImage *)captureFullScreen
{
    AppDelegate *_appDelegate = (AppDelegate *)[UIApplicationsharedApplication].delegate;

    // if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
    // for retina-display
    UIGraphicsBeginImageContextWithOptions(_appDelegate.window.bounds.size, NO, [UIScreen
    mainScreen].scale);
    [_appDelegate.window drawViewHierarchyInRect:_appDelegate.window.bounds 
    afterScreenUpdates:NO];
     screenshot= UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Photo Saved To Camera Roll"
                                                    message:@""
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];

    screenshot=[self centerCropImage:screenshot];

    UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil); //if you need to save
    [alert show];
    return screenshot;
}

1 个答案:

答案 0 :(得分:2)

最有可能的原因是,您在更新同一帧期间同时隐藏和截取屏幕截图。所以你实际截取的屏幕截图是当前显示的帧的屏幕内容,它不显示隐藏按钮之类的任何更改,直到Sprite Kit在更新/动作/物理周期结束时重新显示屏幕。

您必须等待一帧,以便Sprite Kit在删除按钮的情况下渲染场景,然后您可以再次显示按钮。请注意,这将不可避免地导致按钮闪烁。

要等待一帧,应该运行runBlock操作就足够了:

[self runAction:[SKAction runBlock:^{
    UIImage* screenshot = [self captureFullScreen];
}];

如果不够,请在didEvaluateActionsdidSimulatePhysics中执行操作,以确保操作计划在下一帧运行。