在Mac OS X 10.7 Lion上使用核心动画的AVFoundation问题 [英] AVFoundation Issue With Core Animation on Mac OS X 10.7 Lion

查看:598
本文介绍了在Mac OS X 10.7 Lion上使用核心动画的AVFoundation问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Mac OS X 10.7苹果已经推出了AVFoundation,我试图生成一个简单的快速时间电影包含一个动画形状。问题是核心动画不渲染,我最终只有一个空白的黑色视频。下面是我使用的代码。我尝试了许多变化,但没有一个工作。导出状态始终为已完成。



奇怪的是,UI视图包含另一个层设置,方法相同,但添加到AVSynchronizedLayer,

  //注意:composition是一个包含单个视频的AVMutableComposition 
// track(30s of black in 1280 x 720)。

//创建动画层
CALayer * renderAnimLayer = [CALayer layer];
renderAnimLayer.frame = CGRectMake(0,0,1280,720);

// - EDIT:Added animated square(now animates)
renderAnimLayer.backgroundColor = CGColorCreateGenericRGB(0.3,0.0,0.0,0.5);

// - 删除
// [self setupAnimationOnLayer:renderAnimLayer];

CALayer * square = [CALayer layer];
square.backgroundColor = CGColorCreateGenericRGB(0,0,1,0.8);
square.frame = CGRectMake(100,100,100,100);

[CATransaction begin];
[CATransaction setDisableActions:YES];
[CATransaction setAnimationDuration:30.0];

CABasicAnimation * animation = [CABasicAnimation animation];
animation.fromValue = [NSValue valueWithPoint:square.position];
animation.toValue = [NSValue valueWithPoint:CGPointOffset(square.position,800,400)];
animation.removedOnCompletion = NO;
animation.beginTime = AVCoreAnimationBeginTimeAtZero;
animation.duration = 30.0;

[CATransaction commit];

[square addAnimation:animation forKey:@position];
[renderAnimLayer addSublayer:square];
// - 编辑结束

//创建合成
AVMutableVideoCompositionLayerInstruction * layerInstr1 =
[AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstruction];
layerInstr1.trackID = 2;

AVMutableVideoCompositionInstruction * instr = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
instr.timeRange = CMTimeRangeMake(kCMTimeZero,composition.duration);
instr.layerInstructions = [NSArray arrayWithObject:layerInstr1];

AVMutableVideoComposition * renderComp = [AVMutableVideoComposition videoComposition];
renderComp.renderSize = renderAnimLayer.frame.size;
renderComp.frameDuration = CMTimeMake(1,30); //通常1,30
renderComp.instructions = [NSArray arrayWithObject:instr];
renderComp.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithAdditionalLayer:renderAnimLayer asTrackID:2];


//创建导出会话并导出
AVAssetExportSession * exportSession = [AVAssetExportSession exportSessionWithAsset:composition presetName:@AVAssetExportPreset1280x720];
exportSession.outputURL = [NSURL URLWithString:@file:///Users/eric/Desktop/toto.mov];
exportSession.timeRange = CMTimeRangeMake(kCMTimeZero,CMTimeMake(30,1));
exportSession.shouldOptimizeForNetworkUse = YES;
exportSession.video Composition = renderComp;

//看看事情是否已经完成。
[exportSession exportAsynchronouslyWithCompletionHandler:^(){
NSLog(@导出完成,状态:%ld,exportSession.status);
}];

// TODO:一旦一切正常,对象被保留,删除。
while(exportSession.progress< 1.0)
usleep(200000);



发现问题的原因



到@ChristianK的指针。添加蓝色方形动画并看到它的工作后,我得总结,像ChritianK,我的setupAnimationOnLayer是问题。起初我以为这是我设置关键帧动画的方式,但也有效。事实证明,我使用CAShapeLayers和根据这个问题 CAShapeLayers不呈现时renderInContext被调用,这是AVExportSession在我想要的背景中试图做的事情。这也将解释为什么我没有这个问题,看着我的层支持视图设置与相同的调用setupAnimationOnLayer:



感谢你的帮助。

解决方案

部分解决方案



导出简单图层动画,但它不会做的是导出自定义CALayers,其内容由drawInContext:或其委托版本drawLayer:inContext:确定。 CAShapeLayer不会导出其内容,也不会导出MyCustomLayer(就我所见)。



下一步:了解如何导出AVExportSession中的自定义图层。 / p>

With Mac OS X 10.7 Apple has introduced AVFoundation and I'm trying to generate a simple quick time movie containing an animated shape. The problem is that the core animation does not render and I end-up having only a blank "black" video. Below is the code that I used. I've tried many variations but alas none of them work. The export status is always "Completed".

The odd thing is that the UI view contains another layer setup with the same way but added to an AVSynchronizedLayer and it displays just fine and I'm able to scrub back and forth in the animation.

// NOTE: composition is an AVMutableComposition containing a single video 
//       track (30s of black in 1280 x 720).

// Create the animated layer
CALayer *renderAnimLayer = [CALayer layer];
renderAnimLayer.frame = CGRectMake(0, 0, 1280, 720);

// -- EDIT: Added animated square (now animates)
renderAnimLayer.backgroundColor = CGColorCreateGenericRGB(0.3, 0.0, 0.0, 0.5);

// -- Removed
// [self setupAnimationOnLayer:renderAnimLayer];

CALayer *square = [CALayer layer];
square.backgroundColor = CGColorCreateGenericRGB(0, 0, 1, 0.8);
square.frame = CGRectMake(100, 100, 100, 100);

[CATransaction begin];
[CATransaction setDisableActions:YES];
[CATransaction setAnimationDuration:30.0];

CABasicAnimation *animation = [CABasicAnimation animation];
animation.fromValue = [NSValue valueWithPoint:square.position];
animation.toValue = [NSValue valueWithPoint:CGPointOffset(square.position, 800, 400)];
animation.removedOnCompletion = NO;
animation.beginTime = AVCoreAnimationBeginTimeAtZero;
animation.duration = 30.0;

[CATransaction commit];

[square addAnimation:animation forKey:@"position"];
[renderAnimLayer addSublayer:square];
// -- End of Edit

// Create a composition
AVMutableVideoCompositionLayerInstruction *layerInstr1 = 
    [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstruction];
layerInstr1.trackID = 2;

AVMutableVideoCompositionInstruction *instr = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
instr.timeRange = CMTimeRangeMake(kCMTimeZero, composition.duration);
instr.layerInstructions = [NSArray arrayWithObject:layerInstr1];

AVMutableVideoComposition *renderComp = [AVMutableVideoComposition videoComposition];
renderComp.renderSize    = renderAnimLayer.frame.size;
renderComp.frameDuration = CMTimeMake(1, 30); // Normally 1,30
renderComp.instructions  = [NSArray arrayWithObject:instr];
renderComp.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithAdditionalLayer:renderAnimLayer asTrackID:2];


// Create an export session and export
AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:composition presetName:@"AVAssetExportPreset1280x720"];
exportSession.outputURL = [NSURL URLWithString:@"file:///Users/eric/Desktop/toto.mov"];
exportSession.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMake(30, 1));
exportSession.shouldOptimizeForNetworkUse = YES;    
exportSession.videoComposition = renderComp;

// Just see how things have finished.
[exportSession exportAsynchronouslyWithCompletionHandler:^() {
    NSLog(@"Export completed with status: %ld", exportSession.status);
}];

// TODO: remove once everything works and objects have been retained.
while (exportSession.progress < 1.0)
    usleep(200000);

Found Cause of the Issue

Thanks to @ChristianK for the pointer. After adding the blue square animation and seeing it work I had to conclude, like ChritianK, that my setupAnimationOnLayer was the issue. At first I thought it was the way I setup key frame animation but that also worked. It turns out that I was using CAShapeLayers and as per this question CAShapeLayers don't render when renderInContext is called, which is what AVExportSession is trying to do in the background I suppose. This would also explain why I don't have that issue when looking at my layer backed view setup with the same call to setupAnimationOnLayer:.

Thanks to both of you for your help.

解决方案

Partial Solution

The above code now works to export simple layer animations but what it won't do is export custom CALayers the contents of which is determined by drawInContext: or its delegate version drawLayer:inContext:. CAShapeLayer won't export its content and neither will a MyCustomLayer (as far as I could see).

Next step: discover how to export custom layers in an AVExportSession.

这篇关于在Mac OS X 10.7 Lion上使用核心动画的AVFoundation问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆