Search code examples
iosobjective-ccocoa-touchuiimagepickercontrollerlandscape

UIImagePickerController: Check if returned image is in landscape or portrait?


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?


Solution

  • 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");
    }