简体   繁体   中英

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?

Try Below code

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

    }
else   // Portrait
    {

    }

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

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

[[UIDevice currentDevice] orientation]

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];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM