10
votes

The only allowed orientation in my app is landscape. I force this in the project properties under target > summery and only allow portrait in shouldAutorotateToInterfaceOrientation

The problem is when I take a picture while holding the phone horizontally (landscape), the photo will be taken in landscape orientation, while the UI remains in portrait mode.

This means in landscape mode I get a wide image of 3264 x 2448 instead of the tall image (2448 x 3264) I get in portrait mode.

Next the user can crop the image, but if the image is taken in landscape mode I get very ugly stretched images. Adding borders or switching the UI to landscape is not an option.

Is there a way to force the UIImagePickerController to take a photo in portrait dimensions even though the phone is held horizontally (landscape mode)?

I initialize the UIImagePickerController with the following settings:

    imagePicker =[[UIImagePickerController alloc] init];
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
    imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
    imagePicker.showsCameraControls = NO;
    imagePicker.modalPresentationStyle = UIModalPresentationFullScreen;
    imagePicker.wantsFullScreenLayout = YES;
    imagePicker.cameraViewTransform = CGAffineTransformScale(imagePicker.cameraViewTransform,CAMERA_TRANSFORM, CAMERA_TRANSFORM);
    imagePicker.navigationBarHidden = YES;
    imagePicker.toolbarHidden = YES;
    [self presentModalViewController:imagePicker animated:NO];
    imagePicker.cameraOverlayView = self.cameraOverlay;
    imagePicker.cameraFlashMode = UIImagePickerControllerCameraFlashModeOff;
1
HI! I have the same problem. Which of the options offered in the answer have solved your problem?doxsi
@doxsi The stripExif did the trick for me at the time.Dirk de Boer

1 Answers

17
votes

The imageOrientation flag is set on the UIImage depending on which way up the camera is being held when the picture is taken. This is a read-only flag and determines the orientation that iOS will attempt to display the image when you place it into a view.

You could check the orientation before displaying the image, and manipulate the imageView accordingly. Assuming you have an imageView enclosed inside a same-sized view...

- (void) imagePickerImage:(UIImage*)image
{
    if (image.imageOrientation == UIImageOrientationUp ) {
        self.imageView.bounds = CGRectMake
           (0, 0, self.view.bounds.size.height, self.view.bounds.size.width);
        self.imageView.transform = CGAffineTransformMakeRotation(M_PI/2);

    } else if (image.imageOrientation == UIImageOrientationDown) {
        self.imageView.bounds = CGRectMake
           (0, 0, self.view.bounds.size.height, self.view.bounds.size.width);
        self.imageView.transform = CGAffineTransformMakeRotation(-M_PI/2);
    }
    else {
        self.imageView.bounds = self.view.bounds;
        self.imageView.transform = CGAffineTransformIdentity;
    }
    self.imageView.image = image;
}

Another option could be to create a new image from an imageRep: this should strip out the orientation information.

update
The native pixel dimensions of an iPhone photograph are landscape, regardless of the way the phone is held when it is taken. It is only the orientation EXIF flag that is set differently. You cannot alter the EXIF flag but you can strip it out by copying the image bitmap data to a new bitmap image. See also my answers here.

Here is a way to strip out the EXIF data and add in a portrait EXIF flag. You are creating a new UIImage and setting the EXIF flag as you create it. You could use this to flag your landscape images as portrait (UIImageOrientationRight).

- (UIImage*)stripExif:(UIImage*)image
{
    CGImageRef cgImage = image.CGImage;
    UIImage* result = [UIImage imageWithCGImage:cgImage scale:1 
                                    orientation:UIImageOrientationRight];
    return result;
}

update 2
It is misleading to refer to the imageOrientation property of UIImage as an EXIF flag, for two reasons:

(1) image metadata is stored in a dictionary-style structure, within which can be a number of sub-dictionaries, one of which is EXIF. Others include TIFF, IPTC, PNG for example. There is also a general set of tags at the top-level dictionary. The orientation key is NOT in the EXIF section, it is found in the TIFF and IPTC subdictionaries (if present) and also as a top-level key. We tend to use the term EXIF when referring to image metadata as a whole but this is misleading.

(2) Orientation metadata in the image file uses a different set of flag conventions to those that Apple uses in their UIImage.imageOrientation property. Therefore you have to take care if reading this property in order to display the image correctly. Apple evidently uses the image file's orientation metadata to set the UIImage's property, but there is a bit of translation going on.

 Orientation 

 Apple/UIImage.imageOrientation     Jpeg/File kCGImagePropertyOrientation

    UIImageOrientationUp    = 0  =  Landscape left  = 1
    UIImageOrientationDown  = 1  =  Landscape right = 3
    UIImageOrientationLeft  = 2  =  Portrait  down  = 8
    UIImageOrientationRight = 3  =  Portrait  up    = 6

Here is a page showing 'exif' rotation flags. Compare with Apple's UIImage documentation (constants section). See also Apple's CGImageProperties_Reference, especially kCGImagePropertyOrientation

I have loaded a small project onto github logging various UIImage metadata issues