2013-02-26 56 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] 
}