使用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/