4GetFullOf
4GetFullOf

Reputation: 1738

How to detect if image is landscape from UIImagePicker

I use the following code to check but it doesn't work.. always returns portrait even on images taken in landscape

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

Im assuming it doesn't work because the image is actually in portrait when selected so how do I detect when the image has black bars like the following image..enter image description here

Upvotes: 0

Views: 1941

Answers (1)

Lyndsey Scott
Lyndsey Scott

Reputation: 37290

Yeah, in this case your "landscape" image orientation is still "UP" since the image is right-side up.

If you only care about images that are of the camera's 4:3 or 3:4 rectangular aspect ratio, perhaps you can just compare the image width to the image height to see whether it's wider than it is tall or vice versa using UIImage's size property, ex.

if (fishingSpotChosenImage.size.height > fishingSpotChosenImage.size.width) {
    NSLog(@"portrait");
} else {
    NSLog(@"landscape");
}

Upvotes: 5

Related Questions