1

我想在视频上添加图像叠加。
为此,我遵循了本教程。现在我想添加淡入 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 我可以做到这一点,那么请帮我解决这个问题。
请帮我解决这个问题。

4

1 回答 1

2

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

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"];

希望这有效...

于 2015-07-02T13:26:06.153 回答