0

この質問のタイトルはかなり明確です。以下のコードは、トリミング操作を実行し、新しいトリミングされた写真をイメージ ビューに表示します。問題は、画像がトリミングされると、元のソース画像とは異なる向きで表示されることです。何故ですか?そして、私はそれについて何をすべきですか?

-(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.));
    }

https://stackoverflow.com/a/5184134/549273を参照してください

4

2 に答える 2

0

入ってくる画像から向きを取得し、適切な回転で新しい画像を作成するのが最善の策だと思います。

渡された情報辞書からメタデータを取得します。

NSDictionary *metadata = [info objectForKey:UIImagePickerControllerMediaMetadata];
NSNumber *orientation = [metadata objectForKey:@"Orientation"];

次に、を作成するときUIImage

croppedImage = [UIImage imageWithCGImage:subImageRef 
                                   scale:1.0 
                             orientation:[orientation intValue]];

少なくとも、それがあなたの出発点になることを願っています。

于 2012-10-10T10:40:29.530 に答える