この質問のタイトルはかなり明確です。以下のコードは、トリミング操作を実行し、新しいトリミングされた写真をイメージ ビューに表示します。問題は、画像がトリミングされると、元のソース画像とは異なる向きで表示されることです。何故ですか?そして、私はそれについて何をすべきですか?
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self.popoverController dismissPopoverAnimated:true];
NSString *mediaType = [info
objectForKey:UIImagePickerControllerMediaType];
[self dismissModalViewControllerAnimated:YES];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
UIImage *image = [info
objectForKey:UIImagePickerControllerOriginalImage];
CGRect rect = CGRectMake(0, ((image.size.height - image.size.width) / 2), image.size.width, image.size.width);
CGImageRef subImageRef = CGImageCreateWithImageInRect(image.CGImage, rect);
CGRect smallBounds = CGRectMake(rect.origin.x, rect.origin.y, CGImageGetWidth(subImageRef), CGImageGetHeight(subImageRef));
UIGraphicsBeginImageContext(smallBounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage(context, smallBounds, subImageRef);
croppedImage = [UIImage imageWithCGImage:subImageRef];
UIGraphicsEndImageContext();
imageView.image = croppedImage;
*編集
問題の根本原因が EXIF タグの削除であるというコメントに基づいて、次のコードで向きを修正しようとしました。これでも問題は解決しませんが、正しい方向への一歩だと思います。おそらく、誰かが質問に対する新しい答えを提案するのに役立つでしょう.
if (image.imageOrientation == UIImageOrientationLeft) {
NSLog(@"left");
CGContextRotateCTM (context, radians(90));
} else if (image.imageOrientation == UIImageOrientationRight) {
NSLog(@"right");
CGContextRotateCTM (context, radians(-90));
} else if (image.imageOrientation == UIImageOrientationUp) {
NSLog(@"up");
// NOTHING
} else if (image.imageOrientation == UIImageOrientationDown) {
NSLog(@"down");
CGContextRotateCTM (context, radians(-180.));
}