سؤال

I've changed the behavior of my app when it goes to background, so it takes a snapshot, applies a blur effect on it, and then pushes the UIImageView into the rootViewController's subviews. It works perfectly on device (not on simulator...), but when i come back, my navigationController's buttons stops working. If i put the app to the background, then back again i see the controller i tapped on.

Here's my code:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    UINavigationController* topViewController = (UINavigationController*)window.rootViewController.topViewController;
    NSLog(@"%@",topViewController.class);
    UIImage* snapshot = [[topViewController topViewController].view takeSnapshot];
    overlay = [[UIImageView alloc] initWithImage:[snapshot applyDarkEffect]];
    [[topViewController topViewController].view addSubView:overlay];
}

The applyDarkEffect is the category from the apple's example app, and the takeSnapshot is a category i found here on stack overflow.

And the code when my app comes back to life:

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    [overlay removeFromSuperview];
}

The overlay is a property of my app delegate;

The takeSnapshot is in a category for UIView:

- (UIImage *)takeSnapshot {
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [UIScreen mainScreen].scale);

    [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];

    // old style [self.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
} 

Also, my rootViewController is a UINavigationController.

هل كانت مفيدة؟

المحلول

I've found the problem!

The "new" style capture caused the problem: [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];

I had to use the "old" style: [self.layer renderInContext:UIGraphicsGetCurrentContext()]

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top