10

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;
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
Dirk de Boer
  • 365
  • 1
  • 3
  • 15

1 Answers1

17

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

Community
  • 1
  • 1
foundry
  • 31,615
  • 9
  • 90
  • 125
  • Thank you for your answer. The problem however is not how to display the photo, but the dimension in which the photo is taken. For instance when I hold the phone in landscape orientation the resolution will be 3264 x 2448 while in portrait I get 2448 x 3264. I'll change my question to be more clear. I get the idea however that it's not possible to force the camera to take a picture in a specific orientation. – Dirk de Boer Jan 24 '13 at 08:30
  • 1
    @DirkdeBoer Native pixel dimensions are always landscape 3264 x 2448... it is the orientation flag which instructs the NSImage which way up to be displayed. – foundry Jan 24 '13 at 08:49
  • @DirkdeBoer, the only way to get rid of the EXIF orientation flag is to copy the bitmap data from the photo into a new bitmap image. Perhaps this is the solution you are after. – foundry Jan 24 '13 at 09:10
  • actualy, you can alter the EXIF flag and save the image with a different orientation. [setting exif data](http://stackoverflow.com/questions/5125323/problem-setting-exif-data-for-an-image) is a good start to do that. – Jonathan Cichon Jan 24 '13 at 09:16
  • I see. It was a bit confusing because checking the resolution of the image straight from didFinishPickingMediaWithInfo gives a different resolution in landscape then in portrait. This is probably because the returned values already take the imageOrientation into account. It gave me the idea that a low(er) level camera resolution was being changed, but this apparently is not the case. Thanks, I'll accept your answer. – Dirk de Boer Jan 24 '13 at 09:35
  • @DirkdeBoer, also check my update, I've added a method for changing the EXIF flag.. you have to get the CGImageRef and make a new UIImage with it. – foundry Jan 24 '13 at 09:37
  • very nice answer! :) – sterling archer Jan 22 '16 at 23:11