UINavigationBar中的UISegmentedControll奇怪的截图iOS

apa*_*ual 4 screenshot uinavigationbar uisegmentedcontrol ios ios7

我有一个UISegmentedControl内部UINavigationBar看起来像这样:

在此输入图像描述

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

在此输入图像描述

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

在此输入图像描述

到目前为止,我知道状态栏没有出现是正常的,而且Share按钮显示为选中状态,因为实际上它是在截取屏幕截图时选择的,但是不知道发生了什么UISegmentedControl,任何的想法?

PS:我可以分享截图代码,但它是非常简单的标准代码.

UPDATE

这是我使用的截图代码:

- (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;
}
Run Code Online (Sandbox Code Playgroud)

更新2

这是一个重现错误的简单项目:https://github.com/apascual/APSegmentedControlExample

Log*_*gan 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;
}
Run Code Online (Sandbox Code Playgroud)

更新

根据@ 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;
}
Run Code Online (Sandbox Code Playgroud)

  • 你也可以在窗口上调用`drawViewHierarchyInRect:`. (2认同)
  • @LeoNatan - 很高兴知道,我更新了我的答案,就像你建议的那样绘制窗口. (2认同)