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

时间:2011-02-03 00:21:59

标签: iphone ipad photo

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

2 个答案:

答案 0 :(得分:12)

使用imageOrientation个实例的UIImage属性。它将返回these常量之一。

示例:

UIImage * image = //从库中加载

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

答案 1 :(得分:1)

我在运行iOS 5.0的iPhone 4上的几十张实际图片上测试了这段代码,并且能够成功地将它们全部设置为纵向模式。这是您修复/检查的方式

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

这是一种做这种检查的简单方法

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

    }
}