ios - UIImageOrientation 到 CIDetectorImageOrientation

标签 ios orientation face-detection

使用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 的原始值可能在未来改变,虽然这不太可能......仍然是一个很好的做法)你应该包括 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;
    }
}

关于ios - UIImageOrientation 到 CIDetectorImageOrientation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15079864/

相关文章:

ios - 字符串的长度作为字符串的扩展

ios - 带有 UIImage 属性的核心数据 - 使用 UIImage 或 NSData?

opencv - 人脸检测:一次只显示一张脸

android - Galaxy Nexus 人脸检测问题

ios - 适用于 iOS 的 Google Mobile Vision 对源图像大小有任何限制吗?

ios - 在 ios 14 中,文本字段边框样式无法正常工作

ios - 将应用程序从 iOS 5 升级到 iOS 6

pygame - 使用 pygame 更改显示方向

ios - SwiftUI 强制在除一个 View 之外的所有 View 上显示肖像

android - 重新映射传感器坐标