2014-04-01 210 views
0

我想將圖像旋轉90度。一切正常。但動畫運行不順暢?用動畫旋轉圖像?

旋轉編碼

- (void)rotatePhoto { 
    UIImage *rotatedImage; 

    if (finalImage.imageOrientation == UIImageOrientationRight) 
     rotatedImage = [[UIImage alloc] initWithCGImage: finalImage.CGImage 
               scale: 1.0 
             orientation: UIImageOrientationDown]; 
    else if (finalImage.imageOrientation == UIImageOrientationDown) 
     rotatedImage = [[UIImage alloc] initWithCGImage: finalImage.CGImage 
               scale: 1.0 
             orientation: UIImageOrientationLeft]; 
    else if (finalImage.imageOrientation == UIImageOrientationLeft) 
     rotatedImage = [[UIImage alloc] initWithCGImage: finalImage.CGImage 
               scale: 1.0 
             orientation: UIImageOrientationUp]; 
    else 
     rotatedImage = [[UIImage alloc] initWithCGImage: finalImage.CGImage 
                scale: 1.0 
               orientation: UIImageOrientationRight]; 

    [self updateImageViewAnimated:rotatedImage] 
} 

我已經試過類似的東西,但它並不順利。它的旋轉,但我不明白

- (void) updateImageViewAnimated:(UIImage *)image 
{ 

    CABasicAnimation* rotationAnimation; 
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; 
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI /2]; 
    rotationAnimation.duration = 0.7; 
    rotationAnimation.cumulative = YES; 
    [_finalImageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"]; 

    [UIView transitionWithView:_finalImageView 
         duration:0.7       options:UIViewAnimationOptionTransitionNone 
        animations:^{ 
         _finalImageView.image = image; 
        } completion:NULL]; 

} 

回答

0

你是誤解UIImageOrientationRight等等。這些是固有的照片質量(例如,因爲照片是在相機旋轉的情況下拍攝的)。它們與旋轉圖像無關。要用動畫旋轉圖像,只需旋轉圖像視圖 - 即應用旋轉變換。請勿使用transitionWithView:;只需旋轉(animateWithDuration:...)。轉換會告訴你當前的旋轉是什麼,然後你可以根據需要進一步旋轉。

+0

但是,如果只應用旋轉變換,那麼我將旋轉imageview。但我也想獲得旋轉的imagenad保存這個旋轉的圖像到文檔目錄......隨着旋轉變換我會得到90度的旋轉圖像? – Mahavir

+0

不,旋轉圖像視圖不會改變底層圖像。但是,你可以有_separate_代碼來派生底層圖像的旋轉版本,以便保存它。 – matt

+0

好吧然後我將首先應用旋轉變換的視圖與動畫和動畫停止後,我將設置旋轉的圖像到圖像視圖。我對嗎? – Mahavir