2012-02-25 96 views
3

我一直在嘗試使用帶動畫的CALayer在動畫中創建ken burns效果,然後將其保存到視頻文件中。在使用CALayer創建ken burns效果時遇到問題

我有我的圖像層在另一個層是1024x576。所有的動畫都應用到圖像層。

這是迄今爲止代碼:

- (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; 
} 

下面的是我如何調用該方法:

CALayer* animatedLayer = [self buildKenBurnsLayerWithImage:image startPoint:CGPointMake(100, 100) endPoint:CGPointMake(500, 500) fromScale:5.0 toScale:2.0]; 

我遇到的問題是,隨着平移和縮放最終的結果是屏幕上的幾個像素關閉。

如果有人知道如何解決這個問題,我會很感激。

+0

好問題+1回答 – 2012-02-28 10:44:26

+0

如果你不縮放,只是平移,問題是否仍然存在? – Raginmari 2012-07-30 19:52:34

回答

0

所有轉換均適用於錨點。嘗試使用錨點

CGPoint anchor = CGPointMake(0.5f, 0.5f); 

你的「視」規模應該不再右下角(如果是負責動畫幾個像素被關閉),但同樣四面八方。