Force UIImagePickerController to take photo in portrait orientation/dimensions iOS

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 14:09:27
foundry

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!