【问题标题】:UISegmentedControll in UINavigationBar weird screenshot iOSUINavigationBar中的分段控件奇怪的截图iOS
【发布时间】:2014-03-13 22:42:58
【问题描述】:

我在UINavigationBar 中有一个UISegmentedControl,看起来像这样:

在这里您可以看到它在 Storyboard 中的创建方式:

问题是,当我以编程方式创建屏幕截图时,为了分享它,我得到了 UISegmentedControl 没有所选标签的文本,如下所示:

据我所知,状态栏不出现是正常的,并且共享按钮显示为选中状态,因为实际上它是在截屏时选中的,但不知道发生了什么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

【问题讨论】:

  • 你试过我更新的答案了吗?

标签: ios ios7 uinavigationbar screenshot uisegmentedcontrol


【解决方案1】:

您使用的是 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;
}

【讨论】:

  • 谢谢,我已经添加了我正在使用的截图代码......是的,它是iOS 7,但我不只是截取一个视​​图,而是从窗口截取,所以UINavigationBar和 UITabBar 也在上面...
  • 您也可以在窗口上调用drawViewHierarchyInRect:
  • @LeoNatan - 很高兴知道,我更新了我的答案,按照你的建议绘制了窗口。
  • 直到现在我才意识到您已经更新了答案,实际上它是正确的......谢谢!
【解决方案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();

【讨论】:

  • 抱歉,这并不能解决 UISegmentControl 的问题,这只会使 shareButton 不被突出显示...
  • 我试过了,甚至在加载数据时触发了截屏的动作,没有任何按钮的干预,而且一直在发生......
  • 我会,但我没有使用图像,所以可能无法修复它...我添加了一个重现错误的简单项目,请参阅我的第二次更新。
  • 太棒了,这绝对是正确的答案。请对其进行编辑,以使“上次更新”部分更加明显。恭喜,谢谢!
猜你喜欢
  • 2020-08-02
  • 1970-01-01
  • 2012-12-14
  • 1970-01-01
  • 2017-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多