带有阴影的 CAlayer 到 UIImage 错误
CAlayer to UIImage bug with shadow
我正在尝试从 CALayer 渲染 UIImage 并保持阴影,但生成的图像与 CALayer 中的图像不同,为什么?
代码
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
结果 UIImage
我哪里错了?
经过查找和测试,发现是shadowPath的问题,只能通过UIView和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)!)
}
}
我正在尝试从 CALayer 渲染 UIImage 并保持阴影,但生成的图像与 CALayer 中的图像不同,为什么?
代码
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
结果 UIImage
我哪里错了?
经过查找和测试,发现是shadowPath的问题,只能通过UIView和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)!)
}
}