Dzung Le
Dzung Le

Reputation: 51

How to detect current orientation to rotate UIImage?

I am implement taking photo function and I'm having the following issue.

After taking the photo, I have to crop a thumbnail from original image to make an avatar icon. But it's not good if user takes the photo in landscape.

Step:

  1. Turn on camera.
  2. Rotate the iPhone to right (so that the camera is in landscape mode).
  3. Take picture.
  4. I get UIImage with bad orientation. Therefore, thumbnail is not good.

How I can detect the orientation to rotate UIImage?

Upvotes: 0

Views: 2630

Answers (4)

Tanuj
Tanuj

Reputation: 531

Use this:

#pragma mark - Image Picker Controller delegate methods

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

    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    self.imageView.image = chosenImage;

    switch (chosenImage.imageOrientation) {
        case UIImageOrientationUp: //Left
            break;
        case UIImageOrientationDown: //Right
            break;
        case UIImageOrientationLeft: //Down
            break;
        case UIImageOrientationRight: //Up
            break;
        default:
            break;
    }

    [picker dismissViewControllerAnimated:YES completion:NULL];
}

Upvotes: 0

Burhanuddin Sunelwala
Burhanuddin Sunelwala

Reputation: 5343

UIImage has a size property. You can use this to find the height and width of the image.

Therefore, if height > width then image is in portrait else landscape

Upvotes: 1

Hemali Luhar
Hemali Luhar

Reputation: 359

Use this code to get the orientation of your device. According to Device orientation you can set Image orientation

[[UIDevice currentDevice] orientation]

Upvotes: 2

Jaffer Sheriff
Jaffer Sheriff

Reputation: 1564

Try Below code

if (image.size.width > image.size.height ) // Landscape
    {

    }
else   // Portrait
    {

    }

Upvotes: 2

Related Questions