【问题标题】:UIImagePickerController Image OrientationUIImagePickerController 图像方向
【发布时间】:2011-04-13 07:38:49
【问题描述】:

谁能告诉我如何使用 UIImagePickerController(相机和相册)委托确定图像的方向、相应地旋转并将其分配给 UIImageView?

【问题讨论】:

    标签: iphone cocoa-touch uiimagepickercontroller orientation exif


    【解决方案1】:

    不,UIImagePickerController 不能明确地做到这一点。

    如果要根据图片的内容来判断。这是一个难题。如何在不了解图像内容的情况下确定图像的方向?

    我可以建议您检查返回图像的宽度和长度,看看它是纵向还是横向。为了旋转图像,外面有一些库和代码,但我没有尝试太多。你可以看看this blog

    【讨论】:

    • 感谢您的链接。如何获取图像的高度和宽度?
    • 阅读您的链接后,我可能不需要方向,因为链接中提供的调整大小方法将保留方向信息,并且 UIImageView 可以自动补偿方向。
    【解决方案2】:

    Vodkhang 的回答部分不正确 - 现代相机在拍摄照片时会将明确的方向放入图像中。这已经很常见了大约 5 年多。

    我之前使用此答案中的代码通过直接从图片中读取“方向”信息来进行旋转:

    UIImagePickerController camera preview is portrait in landscape app

    【讨论】:

      【解决方案3】:
          // Use this UIImagePickerController's  delegate method
          - (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info {
              // Getting image user just has shoot
              UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
      
              // Fixing to stick with only one orientation (UIImageOrientationUp in this case)
              switch (image.imageOrientation) {
                  case UIImageOrientationDown:
                  case UIImageOrientationDownMirrored:
                  case UIImageOrientationLeft:
                  case UIImageOrientationLeftMirrored:
                  case UIImageOrientationRight:
                  case UIImageOrientationRightMirrored:
                      image = [UIImage imageWithCGImage:image.CGImage
                                                        scale:image.scale
                                                  orientation:UIImageOrientationUp]; // change this if you need another orientation
                      break;
                  case UIImageOrientationUp:
                  case UIImageOrientationUpMirrored:
                      // The image is already in correct orientation
                      break;
              }
      
              // Do whatever you want with image (likely pass it to some UIImageView to display)
              UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
      
              // Dismiss UIImagePickerController
              [picker dismissModalViewControllerAnimated:NO];
          }
      

      注意:UIImageView 读取imageOrientation 属性并相应地显示UIImage

      【讨论】:

      • 我的应用是 iPad,只有横向,iOS 8/9。有很多关于在UIImagePickerController 上启用横向的 iOS 7 时代问题(对我来说这是开箱即用的),但没有人解释为什么我在 横向右侧 拍摄的照片显示向上- 在 UIImageView 上向下,而我在 landscape left 中拍摄的那些则没有。谢谢你的回答。
      猜你喜欢
      • 2011-06-26
      • 2011-07-22
      • 1970-01-01
      • 1970-01-01
      • 2015-12-05
      • 2011-12-14
      • 1970-01-01
      相关资源
      最近更新 更多