【问题标题】:How to know if photo in Landscape or Portrait mode?如何知道照片是横向还是纵向模式?
【发布时间】:2011-02-03 00:21:58
【问题描述】:

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

【问题讨论】:

  • 您想知道设备或照片的方向吗?您可以通过 Viren 的回答获得当前的设备方向。否则,比较照片的高度和宽度得到它的纵横比。但是没有办法知道实际的方向。

标签: iphone ipad photo


【解决方案1】:

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

例子:

UIImage *image = // 从库中加载

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

【讨论】:

  • 这帮助我开始选择专辑,我想添加一些用于拍照的补充信息,我没有意识到这是两个完全不同的东西!当用户使用 iPhone 的相机拍摄快照时,图像不是竖直的,而是逆时针旋转了 90 度。因此,如果您想在NSLog(@"portrait");if 块中结束,那么您将测试UIImageOrientationLeft || UIImageOrientationRight || UIImageOrientationLeftMirrored || UIImageOrientationRightMirrored,其余的将是landscape
【解决方案2】:

我在运行 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 ...");

    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-16
    • 2013-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多