UIImagePickerControllerから取得しているUIImageがあります。メソッドから画像を受け取ったら- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
、プレビュー用にUIImageViewに直接配置し、ユーザーに画像を保存するか破棄するかを選択できるようにします。
ユーザーが保存オプションを選択すると、アプリケーションは画像のpngデータを取得し、そのドキュメントディレクトリに保存します。ただし、2つの可能なデバイスの向き(横向きのみ)のいずれかで発生するのは、画像が上下逆に保存されることです(より具体的には、本来あるべき位置から180度回転します)。そのため、ギャラリーに画像を読み込むと、画像が上下逆に表示されます。
(ギャラリーの左下の画像を参照してください)
UIImagePickerControllerからのUIImageの生の画像データは回転せず、方向はオブジェクトのプロパティとして保存され、表示時にのみ適用されるようになりました。そのため、オンラインで見つけたコードを使用して、UIImageに関連付けられたCGImageを回転させようとしています。しかし、それは画像にまったく影響を与えていないようです。私が使用しているコードは次のとおりです。
- (void)rotateImage:(UIImage*)image byRadians:(CGFloat)rads
{
// calculate the size of the rotated view's containing box for our drawing space
UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,image.size.width, image.size.height)];
CGAffineTransform t = CGAffineTransformMakeRotation(rads);
rotatedViewBox.transform = t;
CGSize rotatedSize = rotatedViewBox.frame.size;
// Create the bitmap context
UIGraphicsBeginImageContext(rotatedSize);
CGContextRef bitmap = UIGraphicsGetCurrentContext();
// Move the origin to the middle of the image you want to rotate and scale around the center.
CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);
// Rotate the image context
CGContextRotateCTM(bitmap, rads);
// Now, draw the rotated/scaled image into the context
CGContextScaleCTM(bitmap, 1.0, -1.0);
CGContextDrawImage(bitmap, CGRectMake(image.size.width / 2, image.size.height / 2, image.size.width, image.size.height), [image CGImage]);
image = UIGraphicsGetImageFromCurrentImageContext();
image = [UIImage imageWithCGImage:image.CGImage scale:1.0 orientation:UIImageOrientationDown];
UIGraphicsEndImageContext();
}
この画像の回転を行うためにオンラインで見つけたすべてのコードが機能していないように見えるため、このコードが機能しない理由を知りたいと思います。