iOS UIView 渲染到 UIImage - Apple 的示例代码只工作一次

标签 ios uiview uiimage

在我的应用程序中,我想将 UIView 的 View 呈现为 UIImage。我为此使用了苹果的示例代码,可以在这里找到:https://developer.apple.com/library/ios/#qa/qa2010/qa1703.html

- (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;
}

第一次尝试时效果很好。但是如果稍后我修改 View 内的 UILabel 文本,上面的函数将返回与上次相同的图像。 我检查过标签的文字确实发生了变化。但是渲染图像仍然没有显示变化。

知道为什么会这样吗?

最佳答案

我在这里唯一的建议是不要在您的代码中恢复 CGState。这可能是问题....

也许您可以尝试将 nsstring 绘制到图像中,而不是折叠图像 看这篇文章How to draw NSString

但我的问题仍然是: 如何在一个应用程序中拥有多个窗口? 常见的用途是在其中有一个窗口和多个 View ...? 那么,为什么需要使用更多窗口?

另一个问题是您使用的最后一个窗口是否也是包含您要显示的文本的窗口?因为您只检索最后一个窗口。

假设你有 3 个 View : 一个是黑色的 一个是蓝色的 一个是白色的

如果你遍历它们并将它们渲染到图层中,而最后一个是白色的 -> 你将只得到白色的。因为另外两个是在最后一个下面渲染的。

那么你的窗口是最上面的吗?

否则我不明白,但你绝对可以使用这个片段

UIGraphicsBeginImageContext(view.frame.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

你会注意到实例方法没有找到,但它存在。

您还需要导入 QuartzCore。

关于iOS UIView 渲染到 UIImage - Apple 的示例代码只工作一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15685680/

相关文章:

ios - 删除动画后保留动画 UIView 属性,但不使用类变量?

ios - Xcode IAP 卡在等待上传

ios - 使用 Swift 将对象添加到数组而不重复

ios - 是否可以在不显示 MPMediaPickerController 的情况下显示 ios 中的所有歌曲

iphone - 如何在 iOS 中为 UIImage 像素化/添加像素化效果

iphone - 将 PDF 加载到 UIImage 或 UIImageView 中?

ios - 如何加载图像并根据它的方向 exif 数据旋转它并使用 UIImageOrientationUp exif 数据保存它

ios - 如何制作一个可扩展的 tableview 来显示 iOS 的其余细节?

swift - 使用layoutAnchor填充 super View

iOS UIView 子类 - 从类方法中显示/删除(如 UIAlertView)