How to know if photo in Landscape or Portrait mode?

泪湿孤枕 提交于 2019-11-30 15:07:28

问题


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?


回答1:


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



回答2:


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 ...");

    }
}


来源:https://stackoverflow.com/questions/4881512/how-to-know-if-photo-in-landscape-or-portrait-mode

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!