[英]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: 步:
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
. 您可以使用它来查找image
的height
和width
。
Therefore, if height
> width
then image
is in portrait else landscape 因此,如果height
> width
则image
为纵向,否则为横向
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.