2011-05-08 24 views
3

有什麼方法可以檢測拍攝圖像時手機的方向?iPhone,拍攝時如何檢測圖像的方向

我在UIView上有一個UIImageView,我使用UIImagePicker拍照或從相機膠捲中選擇一張。但是如果圖像是在橫向模式下拍攝的,我想檢測這個圖像並調整圖像視圖的大小來阻止圖像被拉伸並看起來很愚蠢。

有沒有人知道這是可能的(我假設是這樣,因爲照片應用程序這樣做),如果是這樣,我將如何去做在代碼/界面生成器設置?

回答

8

您可以使用myImage.imageOrientation,這將給你的UIImageOrientation,

或者,如果由於某種原因,你想直接的EXIF信息中的「imagePickerController:didFinishPickingMediaWithInfo:」方法。

通過訪問信息字典[infoObjectForKey:@「Orientation」];

注意:這與UIImageOrientation直接相關的關係如下。

 
- (UIImageOrientation)orientationFromEXIF:(int)exif 
{ 
    UIImageOrientation newOrientation; 
    switch (exif) 
    { 
    case 1: 
     newOrientation = UIImageOrientationUp; 
     break; 
    case 3: 
     newOrientation = UIImageOrientationDown; 
     break; 
    case 8: 
     newOrientation = UIImageOrientationLeft; 
     break; 
    case 6: 
     newOrientation = UIImageOrientationRight; 
     break; 
    case 2: 
     newOrientation = UIImageOrientationUpMirrored; 
     break; 
    case 4: 
     newOrientation = UIImageOrientationDownMirrored; 
     break; 
    case 5: 
     newOrientation = UIImageOrientationLeftMirrored; 
     break; 
    case 7: 
     newOrientation = UIImageOrientationRightMirrored; 
     break; 
    } 
    return newOrientation; 
} 

但是你最好不要使用.imageOrientation

+3

的XCode總是給我一個「壞訪問」錯誤,當我嘗試訪問imageOrientation – zakdances 2012-06-08 19:18:33

1

在UIImage中檢出@property(非原子,只讀)UIImageOrientation imageOrientation 。

2

檢查這個代碼在斯威夫特 -

class func DetectOrientation(img : UIImage) -> UIImageOrientation{ 
      var newOrientation = UIImageOrientation.Up 
      switch (img.imageOrientation) 
      { 
      case .Up: 
       newOrientation = UIImageOrientation.Up; 
       break; 
      case .Down: 
       newOrientation = UIImageOrientation.Down; 
       break; 
      case .Left: 
       newOrientation = UIImageOrientation.Left; 
       break; 
      case .Right: 
       newOrientation = UIImageOrientation.Right; 
       break; 
      case .UpMirrored: 
       newOrientation = UIImageOrientation.UpMirrored; 
       break; 
      case .DownMirrored: 
       newOrientation = UIImageOrientation.DownMirrored; 
       break; 
      case .LeftMirrored: 
       newOrientation = UIImageOrientation.LeftMirrored; 
       break; 
      case .RightMirrored: 
       newOrientation = UIImageOrientation.RightMirrored; 
       break; 
      } 
      return newOrientation; 
     } 
+0

&目標C中? – Robert 2016-02-05 11:59:27

相關問題