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

时间:2015-07-06 10:15:09

标签: ios objective-c uiimageview uiimagepickercontroller

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

enter image description here

enter image description here

如果我在UIImage显示UIImageView的方向,请始终获取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 ..

如何解决这个问题,并在我的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];

    } 

然后在ThirdView中将此分配给其他segue之间的UIImageView:     //照片是从segue获得的变量      UIImage * imageToDisplay = photo.fixOrientation;

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

感谢。

2 个答案:

答案 0 :(得分:0)

使用此

的类别

<强>的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

答案 1 :(得分:0)

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

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];