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