i'm trying to render UIImage from CALayer keeping shadows, but the resulting image is different from the image in CALayer, why?
code
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.view.backgroundColor = UIColor.black
let containerLayer = CALayer()
containerLayer.frame = CGRect(x: 100, y: 20, width: 40, height: 200)
let layerGradient = CAGradientLayer()
layerGradient.frame = CGRect(x: 15, y: 0, width: 10, height: 200)
layerGradient.startPoint = CGPoint(x: 0, y: 0.5)
layerGradient.endPoint = CGPoint(x: 1, y: 0.5)
layerGradient.colors = [UIColor.black.cgColor, UIColor.white.cgColor, UIColor.black.cgColor]
layerGradient.shadowOffset = CGSize(width: -3, height: 0)
layerGradient.shadowPath = UIBezierPath(rect: CGRect(x: 0, y: 0, width: 15, height: 200)).cgPath
layerGradient.shadowRadius = 7
layerGradient.shadowColor = UIColor.black.cgColor
layerGradient.shadowOpacity = 1
containerLayer.backgroundColor = UIColor.white.cgColor
containerLayer.addSublayer(layerGradient)
self.view.layer.addSublayer(containerLayer)
let imageSeparator = imageFromLayer(layer: containerLayer)
let img = UIImageView(image: imageSeparator)
self.view.addSubview(img)
}
func imageFromLayer(layer:CALayer) -> UIImage {
UIGraphicsBeginImageContextWithOptions(layer.frame.size, layer.isOpaque, layer.contentsScale)
layer.render(in: UIGraphicsGetCurrentContext()!)
let outputImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return outputImage!
}
CALayer
Result UIImage
where am i wrong?
after searching and testing, it turned out that the problem is in the shadowPath, and you can render it only through UIView and drawhierarchy https://developer.apple.com/documentation/uikit/uiview/1622589-drawhierarchy
extension UIImage{
convenience init(view: UIView) {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.isOpaque, 0.0)
view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.init(cgImage: (image?.cgImage)!)
}
}