如何从UIImagePicker检测图像是否为横向

时间:2014-04-14 19:41:53

标签: ios uiimagepickercontroller

我使用以下代码进行检查,但它不起作用..即使在横向拍摄的图像上也始终返回肖像

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

    //dismiss imagepicker controller
    [self dismissViewControllerAnimated:NO completion:nil];

    fishingSpotChosenImage = [info objectForKey:UIImagePickerControllerOriginalImage];

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

我认为它不起作用,因为图像在选择时实际上是纵向的,所以我如何检测图像何时出现如下图所示的黑条.. enter image description here

1 个答案:

答案 0 :(得分:5)

是的,在这种情况下你的"风景"图像方向仍然是" UP"因为图像正面朝上。

如果您只关心相机的4:3或3:4矩形宽高比的图像,也许您只需将图像宽度与图像高度进行比较,看它是否更宽使用UIImage的尺寸属性(例如

),而不是高,反之亦然
if (fishingSpotChosenImage.size.height > fishingSpotChosenImage.size.width) {
    NSLog(@"portrait");
} else {
    NSLog(@"landscape");
}