OGeek|极客世界-中国程序员成长平台

标题: ios - 通过 AVExportSession 导出带有隐式动画的 CALayer [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:14
标题: ios - 通过 AVExportSession 导出带有隐式动画的 CALayer

我正在尝试通过 AVExportSession 为我的自定义 CALayer 导出一个带有自定义属性的动画,请在下面找到设置:

class CustomAnimatable: CALayer
{
    @NSManaged var brightness: CGFloat

    override init(layer: Any) {
        super.init(layer: layer);

        if let l = layer as? CustomAnimatable {
            self.brightness = l.brightness;
        }
    }

    override func action(forKey event: String) -> CAAction?
    {
        if event == "brightness" {
            let animation = CABasicAnimation(keyPath: event);
            animation.fromValue = presentation()?.brightness ?? self.brightness;
            return animation;
        }

        return super.action(forKey: event);
    }

    override class func needsDisplay(forKey key: String) -> Bool
    {
        if key == "brightness" {
            return true;
        }

        return super.needsDisplay(forKey: key);
    }

    override func display()
    {
        print("\(self) \(presentation()?.brightness) \(self.brightness)")
    }
}

这是导出 session 的预设置:

func render()
{
     ......

    let parentLayer = CALayer();
    let videoLayer = CALayer();
    let animationLayer = CustomAnimatable()

    parentLayer.frame = frame;
    videoLayer.frame = frame;
    animationLayer.frame = frame;

    parentLayer.addSublayer(videoLayer);
    parentLayer.addSublayer(animationLayer);

    CATransaction.begin()
    CATransaction.setAnimationDuration(2.2);
    CATransaction.setDisableActions(true);
    CATransaction.setAnimationTimingFunction(CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear))

    let anim = CABasicAnimation(keyPath: "brightness");

    anim.fromValue = 1.0;
    anim.fillMode = kCAFillModeBoth;
    anim.beginTime = AVCoreAnimationBeginTimeAtZero;
    anim.repeatCount = 1;
    anim.toValue = 0.0;
    anim.isRemovedOnCompletion = false;

    animationLayer.add(anim, forKey: "anim")

    CATransaction.commit()


    let videoComposition = AVMutableVideoComposition();

    videoComposition.renderSize = CGSize(width: width, height: height);
    videoComposition.instructions = [mainInstruction];
    videoComposition.frameDuration = CMTimeMake(1, 30);
    videoComposition.animationTool = AVVideoCompositionCoreAnimationTool(postProcessingAsVideoLayer: videoLayer, in: parentLayer);

     ....
}

问题是生成的视频中的 brightness 值在没有动画的情况下从 1 变为 0。如果我尝试为 CALayer 的原生属性设置动画,例如opacity - 导出的动画视频完全没问题,不透明度从1平滑渐变到0。

我对自定义属性做错了吗?

我考虑过的事情:

我对原生 CALayer 属性的动画效果有点困惑,因此导出 session 设置似乎是正确的。

除此之外,如果我将自定义图层添加到 View 并为 brightness 属性设置动画 - 它的动画效果也很好。所以这个问题似乎特定于使用 AVExportSession 渲染自定义属性动画。



Best Answer-推荐答案


我不完全确定您为什么在 parentLayer? 中同时添加了类 videoLayeranimationLayer

parentLayer.addSublayer(videoLayer);
parentLayer.addSublayer(animationLayer);

使用以下代码希望对你有所帮助!

parentLayer.addSublayer(videoLayer);
videoLayer.addSublayer(animationLayer);

关于ios - 通过 AVExportSession 导出带有隐式动画的 CALayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47181365/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4