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