4

用于检测图像中的人脸,您需要根据CIDetector文档指定恰好在 TIFF 和 EXIF 规范中指定的图像方向,这意味着它不同于UIImageOrientation. 谷歌为我找到了下面的功能,我试过了,但发现它似乎不正确,或者我可能错过了其他东西,因为有时方向是关闭的。有谁知道发生了什么?似乎一旦从 iDevice 导出照片,然后将其导入另一个 iDevice,方向信息就会丢失/更改,从而导致一些方向不匹配。

- (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;
    }
}
4

3 回答 3

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;
    }
}
于 2015-03-24T18:46:24.217 回答
1

在斯威夫特 4

func inferOrientation(image: UIImage) -> CGImagePropertyOrientation {
  switch image.imageOrientation {
  case .up:
    return CGImagePropertyOrientation.up
  case .upMirrored:
    return CGImagePropertyOrientation.upMirrored
  case .down:
    return CGImagePropertyOrientation.down
  case .downMirrored:
    return CGImagePropertyOrientation.downMirrored
  case .left:
    return CGImagePropertyOrientation.left
  case .leftMirrored:
    return CGImagePropertyOrientation.leftMirrored
  case .right:
    return CGImagePropertyOrientation.right
  case .rightMirrored:
    return CGImagePropertyOrientation.rightMirrored
  }
}
于 2018-05-26T12:41:06.363 回答
0

斯威夫特 4:

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

    return cgiOrientations[orientation.rawValue]
}
于 2017-09-18T05:08:49.287 回答