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?
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");
}
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 ...");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With