【问题标题】:AVVideoCompositionCoreAnimationTool not adding all CALayersAVVideoCompositionCoreAnimationTool 未添加所有 CALayers
【发布时间】:2015-04-20 23:32:56
【问题描述】:

好吧,这个让我完全难住了。如果您需要,我很乐意发布其他代码,但我认为这就足够了。我终其一生都无法弄清楚为什么事情会出错。我正在使用 AVVideoCompositionCoreAnimationTool 将包含图像的 CALayers 添加到合成中。我创建了一个包含我想要添加的所有注释的 NSArray(参见下面的界面),然后使用枚举器将它们添加到动画层。据我所知,无论有多少注释都在数组中,唯一最终出现在输出视频中的是最后一个循环添加的注释。有人能发现我错过了什么吗?

这是注解的接口

@interface Annotation : NSObject// <NSCoding>

@property float time;
@property AnnotationType type;
@property CALayer *startLayer;
@property CALayer *typeLayer;
@property CALayer *detailLayer;

+ (Annotation *)annotationAtTime:(float)time ofType:(AnnotationType)type;

- (NSString *)annotationString;

@end

这是创建带有动画的视频合成的消息。

- (AVMutableVideoComposition *)createCompositionForMovie:(AVAsset *)movie fromAnnotations:(NSArray *)annotations {
 AVMutableVideoComposition *videoComposition = nil;

if (annotations){
//CALayer *imageLayer = [self layerOfImageNamed:@"Ring.png"];
//imageLayer.opacity = 0.0;
//[imageLayer setMasksToBounds:YES];

Annotation *ann;
NSEnumerator *enumerator = [annotations objectEnumerator];

CALayer *animationLayer = [CALayer layer];
animationLayer.frame = CGRectMake(0, 0, movie.naturalSize.width, movie.naturalSize.height);

CALayer *videoLayer = [CALayer layer];
videoLayer.frame = CGRectMake(0, 0, movie.naturalSize.width, movie.naturalSize.height);

[animationLayer addSublayer:videoLayer];

// TODO: Consider amalgamating this message and scaleVideoTrackTime:fromAnnotations
// Single loop instead of two and sharing of othe offset variables

while (ann = (Annotation *)[enumerator nextObject]) {
  CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
  animation.duration = 3; // TODO: Three seconds is the currently hard-coded display length for an annotation, should this be a configurable option after the demo?
  animation.repeatCount = 0;
  animation.autoreverses = NO;
  animation.removedOnCompletion = NO;
  animation.fromValue = [NSNumber numberWithFloat:1.0];
  animation.toValue = [NSNumber numberWithFloat:1.0];
  animation.beginTime = time;
  //  animation.beginTime = AVCoreAnimationBeginTimeAtZero;

  ann.startLayer.opacity = 0.0;
  ann.startLayer.masksToBounds = YES;
  [ann.startLayer addAnimation:animation forKey:@"animateOpacity"];
  [animationLayer addSublayer:ann.startLayer];

  ann.typeLayer.opacity = 0.0;
  ann.typeLayer.masksToBounds = YES;
  [ann.typeLayer addAnimation:animation forKey:@"animateOpacity"];
  [animationLayer addSublayer:ann.typeLayer];

  ann.detailLayer.opacity = 0.0;
  ann.detailLayer.masksToBounds = YES;
  [ann.detailLayer addAnimation:animation forKey:@"animateOpacity"];
  [animationLayer addSublayer:ann.detailLayer];

}

  videoComposition = [AVMutableVideoComposition videoCompositionWithPropertiesOfAsset:movie];

  videoComposition.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:animationLayer];
}

  return videoComposition;
}

我想强调视频输出正确,并且我确实让图层出现在正确的时间,而不是所有图层。非常困惑,非常感谢您的帮助。

【问题讨论】:

    标签: objective-c macos avfoundation calayer avassetexportsession


    【解决方案1】:

    所以我一直在摆弄试图找出可能导致此问题的原因,结果发现这是由设置为 YES 的图层隐藏属性引起的。通过将其设置为 NO,所有图层都会出现,但它们从未消失。所以我不得不将动画的 autoreverses 属性更改为 YES 并将持续时间减半。

    所以我把代码改成了这样:

    while (ann = (Annotation *)[enumerator nextObject]){
      CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
      animation.duration = 1.5; // TODO: Three seconds is the currently hard-coded display length for an annotation, should this be a configurable option after the demo?
      animation.repeatCount = 0;
      animation.autoreverses = YES; // This causes the animation to run forwards then backwards, thus doubling the duration, that's why a 3-second period is using 1.5 as duration
      animation.removedOnCompletion = NO;
      animation.fromValue = [NSNumber numberWithFloat:1.0];
      animation.toValue = [NSNumber numberWithFloat:1.0];
      animation.beginTime = time;
      //  animation.beginTime = AVCoreAnimationBeginTimeAtZero;
    
      ann.startLayer.hidden = NO;
      ann.startLayer.opacity = 0.0;
      ann.startLayer.masksToBounds = YES;
      [ann.startLayer addAnimation:animation forKey:@"animateOpacity"];
      [animationLayer addSublayer:ann.startLayer];
    
      ann.typeLayer.hidden = NO;
      ann.typeLayer.opacity = 0.0;
      ann.typeLayer.masksToBounds = YES;
      [ann.typeLayer addAnimation:animation forKey:@"animateOpacity"];
      [animationLayer addSublayer:ann.typeLayer];
    
      ann.detailLayer.hidden = NO;
      ann.detailLayer.opacity = 0.0;
      ann.detailLayer.masksToBounds = YES;
      [ann.detailLayer addAnimation:animation forKey:@"animateOpacity"];
      [animationLayer addSublayer:ann.detailLayer];
    }
    

    【讨论】:

      猜你喜欢
      • 2012-03-24
      • 1970-01-01
      • 2017-05-25
      • 2016-04-11
      • 2011-06-08
      • 2017-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多