title: having trouble creating ken burns effect with CALayer for iphone
body: <p>I have been trying for the last few days to create the ken burns effect using a CALayer with animations and then save it to a video file.</p>
  
  <p>I have my image layer which is inside another layer that is 1024x576. All of the animations are applied to the image layer.</p>
  
  <p>Here is the code so far:</p>
  
  <pre><code>- (CALayer*)buildKenBurnsLayerWithImage:(UIImage *)image startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint fromScale:(float)fromScale toScale:(float)toScale
  {
      float calFromScale = fromScale + 1;
      float calToScale = toScale + 1;
      float fromX = startPoint.x * calFromScale;
      float fromY = (image.size.height * calFromScale) - (videoSize.height + (startPoint.y * calFromScale));
      float toX = endPoint.x * calToScale;
      float toY = (image.size.height * calToScale) - (videoSize.height + (endPoint.y * calToScale));
  
      CGPoint anchor = CGPointMake(0.0, 0.0);
  
      CALayer* imageLayer    = [CALayer layer];
      imageLayer.contents    = (id)image.CGImage;
      imageLayer.anchorPoint = anchor;
      imageLayer.bounds      = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
      imageLayer.position    = CGPointMake(image.size.width * anchor.x, image.size.height * anchor.y);
      imageLayer.contentsGravity = kCAGravityResizeAspect;
  
      // create the panning animation.
      CABasicAnimation* panningAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
      panningAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(-fromX, -fromY)];
      panningAnimation.toValue   = [NSValue valueWithCGPoint:CGPointMake(-toX, -toY)];
      panningAnimation.additive = YES;
      panningAnimation.removedOnCompletion = NO;
  
      // create the scale animation.
      CABasicAnimation* scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
      scaleAnimation.fromValue = [NSNumber numberWithFloat:fromScale];
      scaleAnimation.toValue = [NSNumber numberWithFloat:toScale];
      scaleAnimation.additive = YES;
      scaleAnimation.removedOnCompletion = NO;
  
      CAAnimationGroup* animationGroup = [CAAnimationGroup animation];
      animationGroup.animations = [[NSMutableArray alloc] initWithObjects:panningAnimation,scaleAnimation, nil];
      animationGroup.beginTime = 1e-100;
      animationGroup.duration = 5.0;
      animationGroup.removedOnCompletion = NO;
      animationGroup.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
  
      [imageLayer addAnimation:animationGroup forKey:nil];
  
      return imageLayer;
  }
  </code></pre>
  
  <p>Here is how i'm calling the method:</p>
  
  <pre><code>CALayer* animatedLayer = [self buildKenBurnsLayerWithImage:image startPoint:CGPointMake(100, 100) endPoint:CGPointMake(500, 500) fromScale:5.0 toScale:2.0];
  </code></pre>
  
  <p>The problem I am having is that the end result with panning and scaling is off by a few pixels on the screen.</p>
  
  <p>If someone knows how to fix this i would great appreciate it.</p>