Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISegmentedControll in UINavigationBar weird screenshot iOS

I have a UISegmentedControl inside a UINavigationBar looking like this:

enter image description here

Here you can see the way it is created in the Storyboard:

enter image description here

The problem is that when I create a screenshot programmatically, in order to share it, I got the UISegmentedControl without the text of the selected tab, like here:

enter image description here

As far I know it is normal that the status bar does not appear, and also that the Share button is shown as selected, because in fact it is selected while the screenshot is being taken, but have no clue about what is happening with the UISegmentedControl, any idea?

PS: I can share the take-screenshot code, but it is pretty straightforward standard code.

UPDATE

This is the screenshot code that I am using:

- (UIImage*)screenshot
{
    // 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;
}

UPDATE 2

Here is a simple project that reproduces the bug: https://github.com/apascual/APSegmentedControlExample

like image 374
apascual Avatar asked Dec 26 '22 12:12

apascual


1 Answers

Are you using iOS 7? If so, are you using the new screenShot api's? If so, have you set afterScreenUpdates to YES?

- (UIImage *) getScreenShotForView:(UIView *)view {
    UIGraphicsBeginImageContext(view.bounds.size);

    // I think this is what was used pre iOS 7.x
    //[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];

    // iOS 7.x -->
    [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES]; // Set To YES
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

Update

Based on @LeoNatan's comment, you can use your window

- (UIImage *) getScreenShotForView:(UIView *)view {
    UIGraphicsBeginImageContext(view.bounds.size);

    // iOS 7.x -->
    [[[[UIApplication sharedApplication] windows] objectAtIndex:0] drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES]; // Set To YES
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}
like image 91
Logan Avatar answered Feb 23 '23 18:02

Logan