使用 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;
    }
}

最佳答案

为了涵盖所有这些,并且不使用魔数(Magic Number)赋值(CGImagePropertyOrientation 的原始值在 future 可能会改变,尽管它不太可能......仍然是一个很好的做法),您应该包含 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;
    }
}

10-08 17:43