2013-02-26 48 views
4

使用CIDetector檢測圖像中的人臉時,需要根據文檔指定恰好在TIFF和EXIF規範中指定的圖像方向,這意味着它不同於UIImageOrientation。谷歌爲我找到了下面的功能,我試了一下,但發現它看起來不正確,或者我可能錯過了其他的東西,因爲有時方向是關閉的。任何人都知道發生了什麼事?看起來,只要一張照片從iDevice導出,然後導入到另一個iDevice,方向信息就會丟失/更改,從而導致一些方向不匹配。UIImageOrientation to CIDetectorImageOrientation

- (int) metadataOrientationForUIImageOrientation:(UIImageOrientation)orientation 
{ 
    switch (orientation) { 
     case UIImageOrientationUp: // the picture was taken with the home button is placed right 
      return 1; 
     case UIImageOrientationRight: // bottom (portrait) 
      return 6; 
     case UIImageOrientationDown: // left 
      return 3; 
     case UIImageOrientationLeft: // top 
      return 8; 
     default: 
      return 1; 
    } 
} 

回答

5

要涵蓋所有,沒有神奇的數字分配這樣​​做(CGImagePropertyOrientation 的原始值可能在未來變化,雖然它不太可能......還是一個很好的做法),你應該包括的ImageIO框架和實際使用的常數:

#import <ImageIO/ImageIO.h> 
- (CGImagePropertyOrientation)CGImagePropertyOrientation:(UIImageOrientation)orientation 
{ 
    switch (orientation) { 
     case UIImageOrientationUp: 
      return kCGImagePropertyOrientationUp; 
     case UIImageOrientationUpMirrored: 
      return kCGImagePropertyOrientationUpMirrored; 
     case UIImageOrientationDown: 
      return kCGImagePropertyOrientationDown; 
     case UIImageOrientationDownMirrored: 
      return kCGImagePropertyOrientationDownMirrored; 
     case UIImageOrientationLeftMirrored: 
      return kCGImagePropertyOrientationLeftMirrored; 
     case UIImageOrientationRight: 
      return kCGImagePropertyOrientationRight; 
     case UIImageOrientationRightMirrored: 
      return kCGImagePropertyOrientationRightMirrored; 
     case UIImageOrientationLeft: 
      return kCGImagePropertyOrientationLeft; 
    } 
} 
0

斯威夫特4:

func convertImageOrientation(orientation: UIImageOrientation) -> CGImagePropertyOrientation { 
    let cgiOrientations : [ CGImagePropertyOrientation ] = [ 
     .up, .down, .left, .right, .upMirrored, .downMirrored, .leftMirrored, .rightMirrored 
    ] 

    return cgiOrientations[orientation.rawValue] 
}