From 6adf5b319b8bd63599340ad9987be2d3c3546997 Mon Sep 17 00:00:00 2001 From: Bob Wakefield Date: Sun, 28 Apr 2024 16:37:49 -0500 Subject: [PATCH] Update view snapshot code. --- FoldingCell/FoldingCell/FoldingCell.swift | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/FoldingCell/FoldingCell/FoldingCell.swift b/FoldingCell/FoldingCell/FoldingCell.swift index 7da21f5..af59ae3 100644 --- a/FoldingCell/FoldingCell/FoldingCell.swift +++ b/FoldingCell/FoldingCell/FoldingCell.swift @@ -485,16 +485,18 @@ extension RotatedView: CAAnimationDelegate { private extension UIView { func takeSnapshot(_ frame: CGRect) -> UIImage? { - UIGraphicsBeginImageContextWithOptions(frame.size, false, 0) - - guard let context = UIGraphicsGetCurrentContext() else { return nil } - context.translateBy(x: frame.origin.x * -1, y: frame.origin.y * -1) - - layer.render(in: context) - let image = UIGraphicsGetImageFromCurrentImageContext() - UIGraphicsEndImageContext() - - return image + if #available(iOS 10.0, *) { + let renderer = UIGraphicsImageRenderer(bounds: bounds) + return renderer.image { rendererContext in + layer.render(in: rendererContext.cgContext) + } + } else { + UIGraphicsBeginImageContext(self.frame.size) + self.layer.render(in:UIGraphicsGetCurrentContext()!) + let image = UIGraphicsGetImageFromCurrentImageContext() + UIGraphicsEndImageContext() + return UIImage(cgImage: image!.cgImage!) + } } }