I am currently working on an Image app where I can put a Text Sticker that made with a UIView. I Applied CATransform3D to change the Perspective of the UIView. When I change the Values, it works properly and changes the Perspective View of the UIView. But when I try to Save the Image It goes to it's default position.
Here is the Code Transformation
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
}
Saving Function
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()
Use drawHierarchy(in: bounds, afterScreenUpdates: true) instead of 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()
}
}
}