iPhone拍摄的竖屏模式图片会被旋转90度。

6

我正在上传从iPhone拍摄的横向和纵向模式的图像。

横向模式的图像上传正常,但是纵向模式的图像上传存在问题,它们会旋转90度。

另外,其他纵向模式的图像(非iPhone拍摄)可以正常工作。

有任何想法为什么会出现这种情况吗?


你要上传到哪里? - shabbirv
我正在将它上传到我的服务器。 - Suresh Varma
实际上,该图像已经旋转,但在 iPhone 和 Mac 上显示正常。 - Suresh Varma
@AppleDeveloper 将您的UIImagePickerController的“allowsEditing”属性设置为是。然后就完成了 :)。请查看下面答案中被接受的第二个选项。 - Suresh Varma
你可以在这里获取优秀的示例代码(但仅限源代码)https://cocoapods.org/pods/UIImage-ImagePickerCrop顺便说一句,这是使用CGAffineTransformRotate的绝佳示例。 - WINSergey
显示剩余5条评论
3个回答

5

在您的代理中:

   - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

当您从“info”字典中获取键为“UIImagePickerControllerOriginalImage”的UIImage后,可以通过imageOrientation属性查看图像方向。如果不符合您的要求,请在上传之前旋转图像。

imageOrientation
The orientation of the receiver’s image. (read-only)

@property(nonatomic, readonly) UIImageOrientation imageOrientation
Discussion
Image orientation affects the way the image data is displayed when drawn. By default, images are displayed in the “up” orientation. If the image has associated metadata (such as EXIF information), however, this property contains the orientation indicated by that metadata. For a list of possible values for this property, see “UIImageOrientation.”

Availability
Available in iOS 2.0 and later.
Declared In
UIImage.h 

UIImage类参考资料

UIImagePickerController类参考资料

UIImagePickerControllerDelegate协议参考资料

第二种选择:

允许用户编辑您的图像并获取“UIImagePickerControllerEditedImage”的图像;

将您的UIImagePickerControllerallowsEditing”属性设置为Yes

在委托中,只需从“info”字典获取UIImage的“UIImagePickerControllerEditedImage”键即可。

祝好运。


这正是我所想的。但我的问题是,当要上传的图像不是从iPhone点击时,一切都运行良好。但只有当它通过iPhone点击时才会旋转。那么问题在于iPhone方向吗?当将图像转换为nsdate时,它是否以旋转的形式提供数据? - Suresh Varma
获取UIImage测试图像方向,如果方向错误则旋转它,然后将其转换为NSData。 - Alex Terente
1
问题在于当我上传一张普通的竖向图片时,它可以正常显示,但是当我上传从iPhone拍摄的竖向图片时,它只会被旋转。此外,如果我在Windows机器上查看该图像,则已经旋转,但是当我在Mac或iPhone上查看相同的图像时,它看起来很正常。 - Suresh Varma
嗨@AlexTerente,如果我在纵向模式下拍摄图像并将其旋转-90度,则会更改图像的大小。 我正在使用下面给出的pattrick代码。 - Paresh Masani
确保CGContextTranslateCTM和CGContextDrawImage使用的尺寸是正确的。 - Alex Terente

4

我曾经苦恼于这个问题很长时间,因为我正在处理一个需要实际旋转图片的项目,也就是说我需要重新排列像素才能上传它。

首先你需要确定图片的方向,然后去掉那些讨厌的元数据,最后再旋转图片。

所以将以下代码放到didFinishPickingMediaWithInfo函数中:

    UIImage * img = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

    if ([info objectForKey:@"UIImagePickerControllerMediaMetadata"]) {
    //Rotate based on orientation
    switch ([[[info objectForKey:@"UIImagePickerControllerMediaMetadata"] objectForKey:@"Orientation"] intValue]) {
        case 3:
            //Rotate image to the left twice.
            img = [UIImage imageWithCGImage:[img CGImage]];  //Strip off that pesky meta data!
            img = [rotateImage rotateImage:[rotateImage rotateImage:img withRotationType:rotateLeft] withRotationType:rotateLeft];
            break;

        case 6:
            img = [UIImage imageWithCGImage:[img CGImage]];
            img = [rotateImage rotateImage:img withRotationType:rotateRight];
            break;

        case 8:
            img = [UIImage imageWithCGImage:[img CGImage]];
            img = [rotateImage rotateImage:img withRotationType:rotateLeft];
            break;

        default:
            break;
    }
}

这里是调整大小函数:

+(UIImage*)rotateImage:(UIImage*)image withRotationType:(rotationType)rotation{
    CGImageRef imageRef = [image CGImage];
    CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
    CGColorSpaceRef colorSpaceInfo = CGColorSpaceCreateDeviceRGB();

    if (alphaInfo == kCGImageAlphaNone)
        alphaInfo = kCGImageAlphaNoneSkipLast;

        CGContextRef bitmap;

    bitmap = CGBitmapContextCreate(NULL, image.size.height, image.size.width, CGImageGetBitsPerComponent(imageRef), 4 * image.size.height/*CGImageGetBytesPerRow(imageRef)*/, colorSpaceInfo, alphaInfo);
    CGColorSpaceRelease(colorSpaceInfo);

    if (rotation == rotateLeft) {
        CGContextTranslateCTM (bitmap, image.size.height, 0);
        CGContextRotateCTM (bitmap, radians(90));
    }
    else{
        CGContextTranslateCTM (bitmap, 0, image.size.width);
        CGContextRotateCTM (bitmap, radians(-90));
    }

    CGContextDrawImage(bitmap, CGRectMake(0, 0, image.size.width, image.size.height), imageRef);
    CGImageRef ref = CGBitmapContextCreateImage(bitmap);
    UIImage *result = [UIImage imageWithCGImage:ref];
    CGImageRelease(ref);
    CGContextRelease(bitmap);
    return result;
}

img变量现在包含一个正确旋转的图像。


0

好的,更干净的版本应该是:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *img = [info valueForKey:UIImagePickerControllerOriginalImage];
    img = [UIImage imageWithCGImage:[img CGImage]];

    UIImageOrientation requiredOrientation = UIImageOrientationUp;
    switch ([[[info objectForKey:@"UIImagePickerControllerMediaMetadata"] objectForKey:@"Orientation"] intValue])
    {
        case 3:
            requiredOrientation = UIImageOrientationDown;
             break;
        case 6:
            requiredOrientation = UIImageOrientationRight;
            break;
        case 8:
            requiredOrientation = UIImageOrientationLeft;
            break;
        default:
            break;
    }

    UIImage *portraitImage = [[UIImage alloc] initWithCGImage:img.CGImage scale:1.0 orientation:requiredOrientation];

}

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