我正在从图像创建视频并向它们添加叠加层。问题是当我尝试将 CATextLayer 添加到视频时。文字像素化,看图
这是用于生成 CATextLayer 的代码:
private func generateTextLayer(for text: String, at frame: CGRect, in layer: CALayer) -> CATextLayer {
let textLayer = CATextLayer()
textLayer.frame = frame.integral
textLayer.contentsScale = UIScreen.main.scale
textLayer.isWrapped = true
textLayer.isHidden = true
textLayer.foregroundColor = UIColor.white.cgColor
textLayer.backgroundColor = UIColor.clear.cgColor
let fontHeight = resolution.fontHeight
textLayer.font = UIFont.systemFont(ofSize: fontHeight)
textLayer.fontSize = fontHeight
textLayer.string = text
textLayer.truncationMode = .end
textLayer.alignmentMode = .center
textLayer.contentsScale = UIScreen.main.scale
layer.addSublayer(textLayer)
return textLayer
}
此代码用于生成带有图像的 CALayer:
private func generateImageLayer(for image: CGImage?, at frame: CGRect, in layer: CALayer) -> CALayer {
let imageLayer = CALayer()
imageLayer.frame = frame.integral
imageLayer.contents = image
imageLayer.contentsGravity = .resizeAspectFill
imageLayer.isHidden = true
imageLayer.backgroundColor = UIColor.clear.cgColor
layer.addSublayer(imageLayer)
return imageLayer
}
由于生成的视频中不同图像层的动画,图像层被隐藏。这是用于层生成的代码:
let size = backgroundTrack.naturalSize.applying(backgroundTrack.preferredTransform)
let parentlayer = CALayer()
parentlayer.frame = CGRect(origin: .zero, size: size).integral
parentlayer.backgroundColor = UIColor.black.cgColor
parentlayer.isOpaque = true
let backgroundVideoLayer = CALayer()
backgroundVideoLayer.frame = parentlayer.bounds
backgroundVideoLayer.backgroundColor = UIColor.black.cgColor
backgroundVideoLayer.isOpaque = true
parentlayer.addSublayer(backgroundVideoLayer)
var currentTime: Double = 0
for source in videoSources {
// This will generate blurred background layer
let blurrLayer = generateImageLayer(for: source.blurredImage, at: backgroundVideoLayer.bounds, in: backgroundVideoLayer)
blurrLayer.contentsGravity = .resizeAspectFill
// Adds image to blurrLayer
if let image = source.image {
let frame = calculateFrame(for: image.size)
let imageLayer = generateImageLayer(for: image.cgImage, at: frame, in: blurrLayer)
imageLayer.isHidden = false
Animations.fadeInOut(layer: blurrLayer, beginTime: currentTime, duration: duration.value)
}
// Adds CATextLayer
if let text = source.title {
let frame = parentlayer.bounds.offsetBy(dx: 0, dy: 100)
let titleLayer = generateTextLayer(for: text, at: frame, in: parentlayer)
Animations.fadeInOut(layer: titleLayer, beginTime: currentTime, duration: duration.value)
}
currentTime += duration.value
}
我尝试将 rasterizationScale 设置为 UIScreen.main.scale,将 shouldRasterize 设置为 true,将 contentsScale 设置为 UIScreen.main.scale,各种不同的选项,甚至尝试拍摄 UILabel 并将其设置为图像(甚至在本地保存图像并检查它,质量很好),但结果又是像素化的书写。
Animate fadeInOut 基本上是三个动画,一个是设置隐藏参数为真或假,另一个是添加 CATransition 显示动画,用于层之间的进入和退出过渡。
static func fadeInOut(layer: CALayer, beginTime: Double, duration: Double, skipEntry: Bool = false) {
let t = beginTime < 1 ? -0.0001 : beginTime
let d = beginTime < 1 ? duration + 0.0001 : duration - 0.0001
if !skipEntry {
let entryTransition = CATransition()
entryTransition.beginTime = t
entryTransition.duration = 0.5
entryTransition.type = CATransitionType.reveal
entryTransition.subtype = CATransitionSubtype.fromTop
entryTransition.isRemovedOnCompletion = false
layer.add(entryTransition, forKey: "entry")
}
let exitTransition = CATransition()
exitTransition.beginTime = d - 0.5
exitTransition.duration = 0.5
exitTransition.startProgress = 1
exitTransition.endProgress = 0
exitTransition.type = CATransitionType.reveal
exitTransition.subtype = CATransitionSubtype.fromTop
exitTransition.isRemovedOnCompletion = false
layer.add(exitTransition, forKey: "exit")
let fadeInOutAnimation = CABasicAnimation(keyPath: "hidden")
fadeInOutAnimation.fromValue = false
fadeInOutAnimation.toValue = false
fadeInOutAnimation.beginTime = t
fadeInOutAnimation.duration = d
fadeInOutAnimation.isRemovedOnCompletion = false
layer.add(fadeInOutAnimation, forKey: "opacity")
}
在将我的问题标记为已经看到并回答之前,我花了很多时间尝试谷歌搜索可能的结果并尝试了很多东西,但结果仍然相同。