将动画叠加图像添加到视频中

时间:2015-06-22 13:05:09

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

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

1 个答案:

答案 0 :(得分: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"];

希望这有效......

相关问题