Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if photo in Landscape or Portrait mode?

Tags:

iphone

ipad

photo

I load photo from iPhone/iPad library, most of them in Portrait mode, I want to know How can I check the photo in Landscape or Portrait mode?

like image 717
mikezang Avatar asked Feb 03 '11 00:02

mikezang


2 Answers

Use the imageOrientation property of UIImage instances. It is going to return you one of these constants.

Example:

UIImage *image = // loaded from library

if (image.imageOrientation == UIImageOrientationUp) {
    NSLog(@"portrait");
} else if (image.imageOrientation == UIImageOrientationLeft || image.imageOrientation == UIImageOrientationRight) {
    NSLog(@"landscape");
}
like image 89
Björn Marschollek Avatar answered Nov 10 '22 23:11

Björn Marschollek


I tested this piece of code on tens of actual picture on iPhone 4 running iOS 5.0 and was able to successfully make them all in portrait mode. This is how you fix/check

if (image.imageOrientation == UIImageOrientationUp ||
        image.imageOrientation == UIImageOrientationDown )
    {
        NSLog(@"Image is in Landscape Fix it to portrait ....");

        backgroundView.frame = self.view.bounds;
        backgroundView.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        backgroundView.contentMode = UIViewContentModeScaleAspectFill;
    }
    else
    {
        NSLog(@"Image is in Portrait everything is fine ...");
    }

Here is a fool proof way of doing this check

-(void)imagePickerController:(UIImagePickerController *)picker
      didFinishPickingImage : (UIImage *)image
                 editingInfo:(NSDictionary *)editingInfo
{

    // Get the data for the image
    NSData* imageData = UIImageJPEGRepresentation(image, 1.0);




    if ([UIImage imageWithData:imageData].size.width >  [UIImage imageWithData:imageData].size.height)
    {
        NSLog(@"Select Image is in Landscape Mode ....");

    }
    else
    {
        NSLog(@"Select Image is in Portrait Mode ...");

    }
}
like image 1
Sam B Avatar answered Nov 10 '22 23:11

Sam B