Reputation: 2980
I have a UISegmentedControl
inside a UINavigationBar
looking like this:
Here you can see the way it is created in the Storyboard:
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:
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
Upvotes: 4
Views: 620
Reputation: 47069
Last updated: You just need to replace
[[window layer] renderInContext:context];
to
[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES]; // also tried with "inRact:window.bounds";
Note - drawViewHierarchyInRect:afterScreenUpdates:
is only for iOS 7
.
For more information, also you can take screenshot just using..
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, YES, [[UIScreen mainScreen] scale]);
[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Upvotes: 2
Reputation: 53112
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;
}
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;
}
Upvotes: 8