你能把当前没有显示的uiview转换成uiimage吗 [英] Can you convert uiview that is not currently being displayed to uiimage

查看:28
本文介绍了你能把当前没有显示的uiview转换成uiimage吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了许多将 UIView 转换为 UIImage 的示例,并且它们在视图布局后可以完美运行等.即使在我的视图控制器中有很多行,其中一些行尚未显示在屏幕上,我也可以做转换.不幸的是,对于表格中太靠下的一些视图(因此尚未绘制"),进行转换会产生一个空白的 UIImage.

I have found numerous examples of converting UIView to UIImage, and they work perfectly for once the view has been laid out etc. Even in my view controller with many rows, some of which are net yet displaying on the screen, I can do the conversion. Unfortunately, for some of the views that are too far down in the table (and hence have not yet been "drawn"), doing the conversion produces a blank UIImage.

我试过调用 setNeedsDisplay 和 layoutIfNeeded,但这些都不起作用.我什至尝试自动滚动表格,但也许我没有以某种方式(使用线程)确保滚动首先发生,从而在转换发生之前更新视图.我怀疑这是不可能的,因为我发现了各种各样的问题,但没有人找到解决方案.或者,我可以只在 UIImage 中重绘我的整个视图,而不需要 UIView 吗?

I've tried calling setNeedsDisplay and layoutIfNeeded, but these don't work. I've even tried to automatically scroll through the table, but perhaps I'm not doing in a way (using threads) that ensures that the scroll happens first, allowing the views to update, before the conversion takes place. I suspect this can't be done because I have found various questions asking this, and none have found a solution. Alternatively, can I just redraw my entire view in a UIImage, not requiring a UIView?

来自保罗哈德森的网站

使用任何未显示在屏幕上的 UIView(比如 UITableview 中位于屏幕底部下方的一行.

Using any UIView that is not showing on the screen (say a row in a UITableview that is way down below the bottom of the screen.

let renderer = UIGraphicsImageRenderer(size: view.bounds.size)
let image = renderer.image { ctx in
    view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)
}

推荐答案

您不必在窗口/屏幕上有视图就可以将其渲染为图像.我在 PixelTest 中完全做到了这一点:

You don't have to have a view in a window/on-screen to be able to render it into an image. I've done exactly this in PixelTest:

extension UIView {

    /// Creates an image from the view's contents, using its layer.
    ///
    /// - Returns: An image, or nil if an image couldn't be created.
    func image() -> UIImage? {
        UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0)
        guard let context = UIGraphicsGetCurrentContext() else { return nil }
        context.saveGState()
        layer.render(in: context)
        context.restoreGState()
        guard let image = UIGraphicsGetImageFromCurrentImageContext() else { return nil }
        UIGraphicsEndImageContext()
        return image
    }

}

如果要在屏幕上渲染,这会将视图的图层渲染为当前看起来的图像.也就是说,如果视图尚未布局,那么它看起来不会像您期望的那样.PixelTest 通过在验证用于快照测试的视图时预先强制布局视图来实现此目的.

This will render a view's layer into an image as it currently looks if it was to be rendered on-screen. That is to say, if the view hasn't been laid out yet, then it won't look the way you expect. PixelTest does this by force-laying out the view beforehand when verifying a view for snapshot testing.

这篇关于你能把当前没有显示的uiview转换成uiimage吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆