UIImagePickerController返回不正确的图像方向

11

我正在使用UIImagePickerController来捕获图像并存储它。但是,当我尝试重新调整其大小时,我得到的方向值是不正确的。当我通过将手机正面朝上拍照时,它给我左侧的方向。有人遇到过这个问题吗?

UIImagePickerController字典显示以下信息:

{
    UIImagePickerControllerMediaMetadata =     {
        DPIHeight = 72;
        DPIWidth = 72;
        Orientation = 3;
        "{Exif}" =         {
            ApertureValue = "2.970853654340484";
            ColorSpace = 1;
            DateTimeDigitized = "2011:02:14 10:26:17";
            DateTimeOriginal = "2011:02:14 10:26:17";
            ExposureMode = 0;
            ExposureProgram = 2;
            ExposureTime = "0.06666666666666667";
            FNumber = "2.8";
            Flash = 32;
            FocalLength = "3.85";
            ISOSpeedRatings =             (
                125
            );
            MeteringMode = 1;
            PixelXDimension = 2048;
            PixelYDimension = 1536;
            SceneType = 1;
            SensingMethod = 2;
            Sharpness = 1;
            ShutterSpeedValue = "3.910431673351467";
            SubjectArea =             (
                1023,
                767,
                614,
                614
            );
            WhiteBalance = 0;
        };
        "{TIFF}" =         {
            DateTime = "2011:02:14 10:26:17";
            Make = Apple;
            Model = "iPhone 3GS";
            Software = "4.2.1";
            XResolution = 72;
            YResolution = 72;
        };
    };
    UIImagePickerControllerMediaType = "public.image";
    UIImagePickerControllerOriginalImage = "<UIImage: 0x40efb50>";
}

但是图片返回的是imageOrientation == 1;

UIImage *picture = [info objectForKey:UIImagePickerControllerOriginalImage];             
6个回答

15

我刚开始在自己的应用程序中解决这个问题。

我使用Trevor Harmon创建的UIImage类别进行图像缩放和修复方向,UIImage+Resize

然后您可以在-imagePickerController:didFinishPickingMediaWithInfo:中执行以下操作:

UIImage *pickedImage = [info objectForKey:UIImagePickerControllerEditedImage];
UIImage *resized = [pickedImage resizedImageWithContentMode:UIViewContentModeScaleAspectFit bounds:pickedImage.size interpolationQuality:kCGInterpolationHigh];

这对我解决了问题。调整大小后的图像在视觉上被正确定位,并且imageOrientation属性报告UIImageOrientationUp。

有几个版本的此比例/调整大小/裁剪代码可用;我使用Trevor的代码,因为它看起来相当干净,并包括我稍后要使用的一些其他UIImage处理器。


这似乎无法与符合 ARC 的代码配合工作。它会因以下错误而失败:"UIImage 的没有可见 @interface 声明选择器 'resizedImageWithContentMode:bounds:interpolationQuality:'"。 - mpemburn
你可以查看由Marc Charbonneau在GitHub上维护的更更新的分支代码。 https://github.com/mbcharbonneau/UIImage-Categories - Bill Garrison
即使使用了Marc的分支,这仍然无法修复方向。 - Will
它总是返回UIImageOrientationUp。因此,它仅适用于UIImageOrientationUp方向,并且不适用于其他方向。 - Keshav

7

我找到了解决方向问题的方法,对我很有效。

UIImage *initialImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSData *data = UIImagePNGRepresentation(self.initialImage);

UIImage *tempImage = [UIImage imageWithData:data];
UIImage *fixedOrientationImage = [UIImage imageWithCGImage:tempImage.CGImage
                                     scale:initialImage.scale
                               orientation:self.initialImage.imageOrientation];
initialImage = fixedOrientationImage;

这将会给你一个收到内存警告的提示。 - Omarj
谢谢,这个解决了我的问题! - Flappy
很遗憾,它不起作用。新图像完全相同,方向错误。 - Eugene Alexeev

6
这是一个高效解决问题的Swift代码片段:
let orientedImage = UIImage(CGImage: initialImage.CGImage, scale: 1, orientation: initialImage.imageOrientation)!

0
我使用以下代码,将其放在一个单独的图像实用程序对象文件中,该文件具有许多其他用于UIImages的编辑方法:
+ (UIImage*)imageWithImage:(UIImage*)sourceImage scaledToSizeWithSameAspectRatio:(CGSize)targetSize
{
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);

    if (CGSizeEqualToSize(imageSize, targetSize) == NO) {
        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;

        if (widthFactor > heightFactor) {
            scaleFactor = widthFactor; // scale to fit height
        }
        else {
            scaleFactor = heightFactor; // scale to fit width
        }

        scaledWidth  = width * scaleFactor;
        scaledHeight = height * scaleFactor;

        // center the image
        if (widthFactor > heightFactor) {
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
        }
        else if (widthFactor < heightFactor) {
            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
        }
    }

    CGImageRef imageRef = [sourceImage CGImage];
    CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
    CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);

    if (bitmapInfo == kCGImageAlphaNone) {
        bitmapInfo = kCGImageAlphaNoneSkipLast;
    }

    CGContextRef bitmap;

    if (sourceImage.imageOrientation == UIImageOrientationUp || sourceImage.imageOrientation == UIImageOrientationDown) {
        bitmap = CGBitmapContextCreate(NULL, targetWidth, targetHeight, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);

    } else {
        bitmap = CGBitmapContextCreate(NULL, targetHeight, targetWidth, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);

    }

    // In the right or left cases, we need to switch scaledWidth and scaledHeight,
    // and also the thumbnail point
    if (sourceImage.imageOrientation == UIImageOrientationLeft) {
        thumbnailPoint = CGPointMake(thumbnailPoint.y, thumbnailPoint.x);
        CGFloat oldScaledWidth = scaledWidth;
        scaledWidth = scaledHeight;
        scaledHeight = oldScaledWidth;

        CGContextRotateCTM (bitmap, M_PI_2); // + 90 degrees
        CGContextTranslateCTM (bitmap, 0, -targetHeight);

    } else if (sourceImage.imageOrientation == UIImageOrientationRight) {
        thumbnailPoint = CGPointMake(thumbnailPoint.y, thumbnailPoint.x);
        CGFloat oldScaledWidth = scaledWidth;
        scaledWidth = scaledHeight;
        scaledHeight = oldScaledWidth;

        CGContextRotateCTM (bitmap, -M_PI_2); // - 90 degrees
        CGContextTranslateCTM (bitmap, -targetWidth, 0);

    } else if (sourceImage.imageOrientation == UIImageOrientationUp) {
        // NOTHING
    } else if (sourceImage.imageOrientation == UIImageOrientationDown) {
        CGContextTranslateCTM (bitmap, targetWidth, targetHeight);
        CGContextRotateCTM (bitmap, -M_PI); // - 180 degrees
    }

    CGContextDrawImage(bitmap, CGRectMake(thumbnailPoint.x, thumbnailPoint.y, scaledWidth, scaledHeight), imageRef);
    CGImageRef ref = CGBitmapContextCreateImage(bitmap);
    UIImage* newImage = [UIImage imageWithCGImage:ref];

    CGContextRelease(bitmap);
    CGImageRelease(ref);

    return newImage; 
}

然后我调用

UIImage *pickedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
        UIImage *fixedOriginal = [ImageUtil imageWithImage:[mediaInfoDict objectForKey:UIImagePickerControllerOriginalImage] scaledToSizeWithSameAspectRatio:pickedImage.size];

0
在iOS 7中,我需要依赖于UIImage.imageOrientation的代码来纠正不同方向的图像。现在,在iOS 8.2中,当我通过UIImagePickerController从相册选择我的旧测试图像时,所有图像的方向都将是UIImageOrientationUp。当我拍照(UIImagePickerControllerSourceTypeCamera)时,这些图像也始终会朝上,无论设备方向如何。 因此,在这些iOS版本之间,显然已经修复了UIImagePickerController如果必要就旋转图像的问题。 甚至可以注意到当相册图像显示时:它们会在原始方向下短暂地显示,然后才以新的朝上方向出现。

0
唯一有效的方法是重新渲染图像,以强制正确的方向。
if (photo.imageOrientation != .up) {
   UIGraphicsBeginImageContextWithOptions(photo.size, false, 1.0);
   let rect = CGRect(x: 0, y: 0, width: photo.size.width, height: photo.size.height);
   photo.draw(in: rect)
   let newImage = UIGraphicsGetImageFromCurrentImageContext()
   UIGraphicsEndImageContext()
   photo = newImage;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接