简体   繁体   English

如何知道横向或纵向模式下的照片?

[英]How to know if photo in Landscape or Portrait mode?

我从iPhone / iPad库加载照片,其中大部分都处于纵向模式,我想知道如何在横向或纵向模式下查看照片?

Use the imageOrientation property of UIImage instances. 使用UIImage实例的imageOrientation属性。 It is going to return you one of these constants. 它将返回这些常量之一。

Example: 例:

UIImage *image = // loaded from library UIImage * image = //从库加载

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. 我在运行iOS 5.0的iPhone 4上的几十张实际图片上测试了这段代码,并且能够成功地将它们全部设置为纵向模式。 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 ...");

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM