CA TextLayer上的视频像素化文本

问题描述 投票:1回答:2

我正在根据图像创建视频,并在其上添加叠加层。问题是当我尝试将CATextLayer添加到视频时。文本已像素化,请查看图像

image

这是用于生成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,应该将rasterize设置为true,contentsScale设置为UIScreen.main.scale,各种不同的选项,甚至尝试对UILabel进行快照并将其设置为图像(甚至本地保存并检查图像,质量很好),但结果再次是像素化写作。

Animate fadeInOut基本上是三种动画,一种是将隐藏参数设置为true或false,另一种是添加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")
}

在将我的问题标记为已经看到并回答之前,我花了很多时间尝试搜索可能的结果,并尝试了很多事情,但是结果仍然相同。

swift calayer avkit catextlayer avvideocomposition
2个回答
1
投票

尝试在.contentsScale属性附近播放。我认为视频以videoComposition的比例呈现,而不是UIScreen.main.scale


0
投票

因此,为了在生成视频时获得高质量的图像或文本,必须注意contentScale将其设置为1。我遇到的另一个问题是某些框架的问题,其中UIImageView或UITextField可以缩放所有内容以使其看起来清晰,而contentMode可以处理所有事情,而使用视频逻辑生成视频开发者必须特别注意这一点

我的意思是,如果图像比图层框架大,图像将缩小,但看起来像素化。为图层提供与图像相同的帧将使图像看起来不错。这在水印图像上尤为明显,在水印图像中,我们将图像放置到具有1x,2x和3x尺寸的资源中。使用UIImage(名称为“ my_watermark”)获取此图像后,它将加载3x尺寸,由于视频比例== 1,我们需要1x尺寸的视频。删除2x和3x尺寸可以解决此问题。

[当我们发现这一点时,我们对CATextLayer进行了相同的测试,因此将contentScale设置为1,并注意框架,就可以了。

通过遵循此逻辑生成的视频在内部具有高质量的文本和图像。最大的问题实际上是在播放设置后需要检查输出结果的时间,在这种情况下,生成视频会花费一些时间,然后将其传输到更大的屏幕上以检查所有内容都非常耗时。

希望此答案对某人有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.