UISegmentedControl
内UINavigationBar
看起来像这样:
在这里,您可以看到在故事板中创建它的方式:
问题在于,当我以编程方式创建屏幕截图时,为了共享它,我得到的UISegmentedControl
没有所选标签的文本,如下所示:
到目前为止,我知道状态栏没有显示是正常的,而且Share按钮显示为选中状态,因为实际上它是在截屏时选择的,但不知道发生了什么有UISegmentedControl
,有什么想法吗?
PS:我可以分享截图代码,但这是非常简单的标准代码。
更新
这是我正在使用的屏幕截图代码:
- (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;
}
更新2
这是一个重现错误的简单项目:https://github.com/apascual/APSegmentedControlExample
答案 0 :(得分:8)
您使用的是iOS 7吗?如果是这样,你使用新的screenShot api?如果是这样,您是否将AfterScreenUpdates设置为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;
}
根据@ LeoNatan的评论,你可以使用你的窗口
- (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;
}
答案 1 :(得分:2)
上次更新:您只需要替换
[[window layer] renderInContext:context];
到
[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES]; // also tried with "inRact:window.bounds";
注意 - drawViewHierarchyInRect:afterScreenUpdates:
仅适用于iOS 7
。
有关详细信息,您也可以使用..
截取屏幕截图UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, YES, [[UIScreen mainScreen] scale]);
[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();