CATransformation3D 转换标签未保存在图像上

CATransformation3D Transformed Label not saving on image

我目前正在开发一个图像应用程序,我可以在其中放置使用 UIView 制作的文本贴纸。我应用 CATransform3D 来改变 UIView 的透视图。当我更改值时,它可以正常工作并更改 UIView 的透视视图。但是当我尝试保存图像时,它会转到默认位置。

这是代码 转型

func transformation ()
{
    var transform = CATransform3DIdentity;
    transform.m34 = 1.0 / -200
    transform = CATransform3DRotate(transform, CGFloat(sliderX.value), 0, 1, 0)
    transform = CATransform3DRotate(transform, CGFloat(sliderY.value), 1, 0, 0)
    textStickerView.currentlyEditingLabel.layer.transform = transform
}

保存功能

var imgSize = CGSize(width: mainImageView.bounds.size.width, height: mainImageView.bounds.size.height)

    UIGraphicsBeginImageContextWithOptions(imgSize, false, UIScreen.main.scale)
    mainImageView.addSubview(textStickerView)
    mainImageView.layer.render(in: UIGraphicsGetCurrentContext()!)
    let rowImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

使用 drawHierarchy(in: bounds, afterScreenUpdates: true) 而不是 mainImageView.layer.render(in: UIGraphicsGetCurrentContext()!)

extension UIView {
func asImage() -> UIImage {
        if #available(iOS 10.0, *) {
            let format = UIGraphicsImageRendererFormat()
            format.opaque = isOpaque
            format.scale = 1.0
            let renderer = UIGraphicsImageRenderer(bounds: bounds, format: format)
            return renderer.image { rendererContext in
                //layer.render(in: rendererContext.cgContext) // can not save transform3D
                drawHierarchy(in: bounds, afterScreenUpdates: true)
            }
        } else {
            UIGraphicsBeginImageContextWithOptions(bounds.size, true, 1.0)
            //self.layer.render(in:UIGraphicsGetCurrentContext()!) // can not save transform3D
            drawHierarchy(in: frame, afterScreenUpdates: true)
            let image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return image ?? UIImage()
        }
    }
}