UIImagePickerController: Check if returned image i

2019-08-07 05:50发布

问题:

This question already has an answer here:

  • How to know if photo in Landscape or Portrait mode? 2 answers

So, the UIImagePickerController does not support landscape orientation. However, there is a portion of application where it is essential that the image that comes from the controller is recognized to be either in a portrait or landscape format.

I can handle this so far when importing an image from my library using the UIImagePickerController. I just compare the UIImage's width and height. However, when I am capturing the image using the UIImagePickerController, this functionality won't work as is.

Is there some extra information that the UIImagePickerController can provide me to determine if it was snapped in a landscape mode? Is there a creative way in emulating this functionality for snapping photos using the UIImagePickerController?

回答1:

I used this to check and fix orientation if u need to fix it.

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

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

    case UIImageOrientationRight:
    case UIImageOrientationRightMirrored:
        transform = CGAffineTransformTranslate(transform, 0, imageSize.height);
        transform = CGAffineTransformRotate(transform, -M_PI_2);
        break;
    case UIImageOrientationUp:
    case UIImageOrientationUpMirrored:
        break;
}
/////////////// or u just do it in this way:
if (image.imageOrientation == UIImageOrientationUp) {
    NSLog(@"portrait");
} else if (image.imageOrientation == UIImageOrientationLeft || image.imageOrientation == UIImageOrientationRight) {
    NSLog(@"landscape");
}