Ios 如何避免错误的方向显示图片,然后使用UIImagePickerController?

Ios 如何避免错误的方向显示图片,然后使用UIImagePickerController?,ios,objective-c,uiimageview,uiimagepickercontroller,Ios,Objective C,Uiimageview,Uiimagepickercontroller,我正在使用UIImagePickerController在Objective C应用程序中拍照。然后用相机拍照或从gallery加载此图片,我在UIImageView中显示此图片。这是我的问题,图片显示旋转90º,如果这是在纵向定向设备 如果我获取了要在UIImageView中显示的UIImage的方向,则始终获取UIImageOrientationUp,因此我无法手动旋转该方向 if (photo.imageOrientation == UIImageOrientationUp) {

我正在使用UIImagePickerController在Objective C应用程序中拍照。然后用相机拍照或从gallery加载此图片,我在
UIImageView
中显示此图片。这是我的问题,图片显示旋转90º,如果这是在纵向定向设备

如果我获取了要在
UIImageView
中显示的
UIImage
的方向,则始终获取
UIImageOrientationUp
,因此我无法手动旋转该方向

 if (photo.imageOrientation == UIImageOrientationUp) {
    NSLog(@"UP"); //Always get this line. If Uiimage is portrait or landscape
}else if (photo.imageOrientation == UIImageOrientationRight) {
    NSLog(@"Right");
}
我正在尝试使用以下函数:

 - (UIImage *)fixrotation:(UIImage *)image{
...
}
但是图片没有旋转。因为我总是得到
UIImageOrientationUp

如何解决此问题并在my
UIImageView
中始终以纵向位置显示我的图片

我的didFinishPickingMediaWithInfo是:

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

image = [info valueForKey:UIImagePickerControllerOriginalImage];

 //I use this image to show in next View
SecondView * controller = [SecondView new];
controller.imageView.image = image;
[self performSegueWithIdentifier:@"segue2" sender:self];
[picker.self dismissModalViewControllerAnimated:YES];

    } 
然后在第三个视图中,在其他序列之间将其分配给UIImageView: //photo是从segue获取的变量 UIImage*imageToDisplay=photo.fixOrientation

self.imageV.image = imageToDisplay;
self.imageV.contentMode = UIViewContentModeScaleAspectFill;
谢谢。

为此使用类别

UIImage+fixOrientation.h

@interface UIImage (fixOrientation)

- (UIImage *)fixOrientation;

@end
UIImage+fixOrientation.m

@implementation UIImage (fixOrientation)

- (UIImage *)fixOrientation {

// No-op if the orientation is already correct
if (self.imageOrientation == UIImageOrientationUp) return self;

// We need to calculate the proper transformation to make the image upright.
// We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
CGAffineTransform transform = CGAffineTransformIdentity;

switch (self.imageOrientation) {
    case UIImageOrientationDown:
    case UIImageOrientationDownMirrored:
        transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height);
        transform = CGAffineTransformRotate(transform, M_PI);
        break;

    case UIImageOrientationLeft:
    case UIImageOrientationLeftMirrored:
        transform = CGAffineTransformTranslate(transform, self.size.width, 0);
        transform = CGAffineTransformRotate(transform, M_PI_2);
        break;

    case UIImageOrientationRight:
    case UIImageOrientationRightMirrored:
        transform = CGAffineTransformTranslate(transform, 0, self.size.height);
        transform = CGAffineTransformRotate(transform, -M_PI_2);
        break;
    case UIImageOrientationUp:
    case UIImageOrientationUpMirrored:
        break;
}

switch (self.imageOrientation) {
    case UIImageOrientationUpMirrored:
    case UIImageOrientationDownMirrored:
        transform = CGAffineTransformTranslate(transform, self.size.width, 0);
        transform = CGAffineTransformScale(transform, -1, 1);
        break;

    case UIImageOrientationLeftMirrored:
    case UIImageOrientationRightMirrored:
        transform = CGAffineTransformTranslate(transform, self.size.height, 0);
        transform = CGAffineTransformScale(transform, -1, 1);
        break;
    case UIImageOrientationUp:
    case UIImageOrientationDown:
    case UIImageOrientationLeft:
    case UIImageOrientationRight:
        break;
}

// Now we draw the underlying CGImage into a new context, applying the transform
// calculated above.
CGContextRef ctx = CGBitmapContextCreate(NULL, self.size.width, self.size.height,
                                         CGImageGetBitsPerComponent(self.CGImage), 0,
                                         CGImageGetColorSpace(self.CGImage),
                                         CGImageGetBitmapInfo(self.CGImage));
CGContextConcatCTM(ctx, transform);
switch (self.imageOrientation) {
    case UIImageOrientationLeft:
    case UIImageOrientationLeftMirrored:
    case UIImageOrientationRight:
    case UIImageOrientationRightMirrored:
        // Grr...
        CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage);
        break;

    default:
        CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage);
        break;
}

// And now we just create a new UIImage from the drawing context
CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
UIImage *img = [UIImage imageWithCGImage:cgimg];
CGContextRelease(ctx);
CGImageRelease(cgimg);
return img;
}

@end

我也面临着这个问题。 此代码必须解决以下问题:

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

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

我使用了你的类,但这不适用于fixorientation。我导入:#导入“UIImage+fixOrientation.h”,然后调用函数到我的photo变量->UIImage*imageToDisplay=photo.fixOrientation;但这不起作用。。我是否做了一些不好的事情??我正在使用NSLog进行调试,如果(self.imageOrientation==UIImageOrientationUp){尽管图片是在纵向位置拍摄的:(尝试添加shouldAutoRotate()方法(返回否)在您的ViewControlleri中,如果我在纵向中打开选取器,就像元数据一样,获取此信息,然后在拍摄时显示此获取位置。能否显示您的UIImagePickerDelegateMethod didFinishPickingMediaWithInfo以及您在UIImageView中设置图像的位置?谢谢,您的图像将显示在SecondV上的UIImageView上View viewController?在故事板上,您可以检查第二个视图的方向是否设置为横向(在该VC的属性面板内)。如果设置为横向,则这就是为什么它将显示在横向(从主VC旋转90*.不,是正确的,所有视图都是纵向的。但我看到了一些尝试,如果我在DIDFINish中使用fixOrientation,单击它会显示图片具有UIImageOrientationRight。但是如果我应用-(UIImage*)fixOrientation,它在下一个视图中保持不旋转。