尝试使用 UIGraphicsImageRenderer 拍摄 uiview 的快照时出现奇怪的错误

Weird error when trying to take a snapshot of a uiview using UIGraphicsImageRenderer

调用此扩展程序时出现以下错误:

extension UIView {
    
    /// Create image snapshot of view.
    func snapshot(of rect: CGRect? = nil) -> UIImage {
        return UIGraphicsImageRenderer(bounds: rect ?? bounds).image { _ in
            drawHierarchy(in: bounds, afterScreenUpdates: true)
        }
    }
}

错误:

vImageConvert_AnyToAny - failed width = 0 height = 1 dst component = 16bit float dstLayout = ByteOrder16Little dstBytesPerRow = 0 src component = 16bit integer srcLayout = ByteOrder16Little srcBytesPerRow = 0

知道为什么会这样吗?想知道颜色是否 space 问题...在模拟器上工作正常,在物理设备上中断。

为其他需要类似东西的人找到了解决方案:

extension UIView {
    
    /// Create image snapshot of view.
    func snapshot(of rect: CGRect? = nil) -> UIImage {
        
        let renderer = UIGraphicsImageRenderer(bounds: rect!)
                return renderer.image { (context) in
                    self.layer.render(in: context.cgContext)
                }
    }
}