【问题标题】:Add animated overlay image to video将动画叠加图像添加到视频
【发布时间】:2015-09-07 22:50:38
【问题描述】:

我想在视频上添加图像叠加。
为此,我遵循了this 教程。现在我想添加淡入 1 秒并在 5 秒后淡出 1 秒。为此,我编写了以下代码。

CALayer *overlayLayer1 = [CALayer layer];
[overlayLayer1 setContents:(id)[overlayImage CGImage]];
overlayLayer1.frame = CGRectMake(0, 0, size.width, size.height);
[overlayLayer1 setMasksToBounds:YES];

CALayer *overlayLayer2 = [CALayer layer];
[overlayLayer2 setContents:(id)[overlayImage CGImage]];
overlayLayer2.frame = CGRectMake(0, 0, size.width, size.height);
[overlayLayer2 setMasksToBounds:YES];

CALayer *overlayLayer3 = [CALayer layer];
[overlayLayer3 setContents:(id)[overlayImage CGImage]];
overlayLayer3.frame = CGRectMake(0, 0, size.width, size.height);
[overlayLayer3 setMasksToBounds:YES];

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
animation.duration=1.0;
animation.repeatCount=1;
animation.autoreverses=NO;
animation.fromValue=[NSNumber numberWithFloat:0.0];
animation.toValue=[NSNumber numberWithFloat:1.0];
animation.beginTime = AVCoreAnimationBeginTimeAtZero;
[overlayLayer1 addAnimation:animation forKey:@"animateOpacity"];

animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
animation.duration=5.0;
animation.repeatCount=1;
animation.autoreverses=NO;
animation.fromValue=[NSNumber numberWithFloat:1.0];
animation.toValue=[NSNumber numberWithFloat:1.0];
animation.beginTime = CACurrentMediaTime() + 1.0;
[overlayLayer2 addAnimation:animation forKey:@"animateOpacity"];

animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
animation.duration=1.0;
animation.repeatCount=1;
animation.autoreverses=NO;
animation.fromValue=[NSNumber numberWithFloat:1.0];
animation.toValue=[NSNumber numberWithFloat:0.0];
animation.beginTime = CACurrentMediaTime() + 6.0;
[overlayLayer3 addAnimation:animation forKey:@"animateOpacity"];

[parentLayer addSublayer:overlayLayer1];
[parentLayer addSublayer:overlayLayer2];
[parentLayer addSublayer:overlayLayer3];
composition.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];

但是当我运行这段代码时,没有用于图像叠加的动画。 始终只显示图像。

我在这个项目中将 GPUImage 用于其他一些操作。 如果通过 GPUImage 我可以做到这一点,那么请帮我解决这个问题。
请帮我解决这个问题。

【问题讨论】:

    标签: ios objective-c overlay video-processing video-editing


    【解决方案1】:

    我认为您的问题是您正在为同一个键添加动画。
    试试下面的代码,我建议你为动画创建另一个对象并使用另一个键。

    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    animation.duration=1.0;
    animation.repeatCount=1;
    animation.autoreverses=NO;
    animation.fromValue=[NSNumber numberWithFloat:0.0];
    animation.toValue=[NSNumber numberWithFloat:1.0];
    animation.beginTime = AVCoreAnimationBeginTimeAtZero;
    animation.removedOnCompletion=NO;
    animation.fillMode = kCAFillModeForwards;
    [overlayLayer1 addAnimation:animation forKey:@"animateOpacity"];
    
    CABasicAnimation * animation1 = [CABasicAnimation animationWithKeyPath:@"opacity"];
    animation1.duration=1.0;
    animation1.repeatCount=1;
    animation1.autoreverses=NO;
    animation1.fromValue=[NSNumber numberWithFloat:1.0];
    animation1.toValue=[NSNumber numberWithFloat:0.0];
    animation1.beginTime = AVCoreAnimationBeginTimeAtZero+6.0;
    animation1.fillMode = kCAFillModeForwards;
    animation1.removedOnCompletion=NO;
    
    [overlayLayer1 addAnimation:animation1 forKey:@"animateOpacity1"];
    

    希望这行得通...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 2021-03-16
      • 1970-01-01
      • 1970-01-01
      • 2017-02-07
      • 1970-01-01
      • 2012-11-03
      相关资源
      最近更新 更多