简体   繁体   English

如何检测当前方向以旋转UIImage?

[英]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). 向右旋转iPhone(以使相机处于横向模式)。
  3. Take picture. 拍照片。
  4. I get UIImage with bad orientation. 我得到方向不好的UIImage。 Therefore, thumbnail is not good. 因此,缩略图效果不好。

How I can detect the orientation to rotate UIImage? 如何检测旋转UIImage的方向?

Try Below code 尝试以下代码

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

    }
else   // Portrait
    {

    }

UIImage has a size property. UIImage具有size属性。 You can use this to find the height and width of the image . 您可以使用它来查找imageheightwidth

Therefore, if height > width then image is in portrait else landscape 因此,如果height > widthimage为纵向,否则为横向

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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