title: AVFoundation Issue With Core Animation on Mac OS X 10.7 Lion
body: <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".</p>
  
  <p>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.</p>
  
  <pre><code>// 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 &lt; 1.0)
      usleep(200000);
  </code></pre>
  
  <h2>Found Cause of the Issue</h2>
  
  <p>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 <a href="https://stackoverflow.com/questions/1282977/cashaperlayer-renderincontext-doesnt-work">this question</a> 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:.</p>
  
  <p>Thanks to both of you for your help.</p>