繁体   English   中英

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

[英]How to detect current orientation to rotate UIImage?

我正在实现拍照功能,并且遇到以下问题。

拍照后,我必须从原始图像中裁剪缩略图以制作头像图标。 但是,如果用户以横向拍摄照片,那就不好了。

步:

  1. 打开相机。
  2. 向右旋转iPhone(以使相机处于横向模式)。
  3. 拍照片。
  4. 我得到方向不好的UIImage。 因此,缩略图效果不好。

如何检测旋转UIImage的方向?

尝试以下代码

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

    }
else   // Portrait
    {

    }

UIImage具有size属性。 您可以使用它来查找imageheightwidth

因此,如果height > widthimage为纵向,否则为横向

使用此代码获取设备的方向。 根据设备方向可以设置图像方向

[[UIDevice currentDevice] orientation]

用这个:

#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