2016-06-29 3 views
1

私のコードでイメージローテーションを実装し、いくつかのテストを実行しようとしています。ローテーションがどのように実行されるかについて私はあまり知らないので、インターネットで見つけたコードを実装しました。イメージの回転によってピクセルが失われるのですか?(コアグラフィックス)

イメージを回転するたびに、エッジの周りの1ピクセルの行全体または列が失われることがわかりました。 (一度にM_PI度以上)

イメージをUIImageオブジェクトとしてホストすると、これは明らかではありませんが、UIImageをファイルとして保存すると表示できます。私はこの損失が起こることを理解していない https://github.com/asldkfjwoierjlk/UIImageRotationTest/tree/master

は、ここに私のテストコードリンクです。私は何か見落としてますか?それとも数学的に不可避なことなのでしょうか?

興味のある方は、私が実装したローテーション方法を以下に示します。

- (UIImage*)rotateImg:(UIImage*)srcImg 
{ 
    UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0, srcImg.size.width, srcImg.size.height)]; 
CGFloat rotationInDegree = ROTATION_DEGREE; 

CGAffineTransform t = CGAffineTransformMakeRotation(rotationInDegree); 
rotatedViewBox.transform = t; 
CGSize rotatedSize = rotatedViewBox.frame.size; 


// set opaque option to NO to leave the alpha channel intact. 
// Otherwise, there's no point of saving to either png or jpg. 
UIGraphicsBeginImageContextWithOptions(rotatedSize, NO, 1.0); 

CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextTranslateCTM(context, rotatedSize.width/2.0f, rotatedSize.height/2.0f); 
CGContextRotateCTM(context, rotationInDegree); 
[srcImg drawInRect:CGRectMake(-srcImg.size.width/2.0f, -srcImg.size.height/2.0f, srcImg.size.width, srcImg.size.height)]; 

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 


return newImage; 
} 

答えて

1

あなたはUIImageViewをビューとして扱うことができます。このコードは私にとって完璧に機能します!

CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; 
NSNumber *currentAngle = [rotatedViewBox.layer.presentationLayer valueForKeyPath:@"transform.rotation"]; 
rotationAnimation.fromValue = currentAngle; 
rotationAnimation.toValue = @(50*M_PI); 
rotationAnimation.duration = 50.0f;    // this might be too fast 
rotationAnimation.repeatCount = HUGE_VALF;  // HUGE_VALF is defined in math.h so import it 
[rotatedViewBox.layer addAnimation:rotationAnimation forKey:@"rotationAnimationleft"]; 

ハッピーコーディング!

関連する問題