How do I know if a photo is in landscape or portrait mode?

I’m downloading a photo from the iPhone / iPad library, most of them are in Portrait mode, I want to know. How to check a photo in landscape or portrait mode?

+8
iphone ipad photo
source share
2 answers

Use the imageOrientation property of imageOrientation instances. He will return one of these constants to you.

Example:

UIImage * image = // loaded from library

 if (image.imageOrientation == UIImageOrientationUp) { NSLog(@"portrait"); } else if (image.imageOrientation == UIImageOrientationLeft || image.imageOrientation == UIImageOrientationRight) { NSLog(@"landscape"); } 
+12
source share

I tested this piece of code on dozens of actual images 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 dumb way to do 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 ..."); } } 
+1
source share

All Articles