【问题标题】:UIImageOrientation to CIDetectorImageOrientationUIImageOrientation 到 CIDetectorImageOrientation
【发布时间】:2013-02-11 08:35:14
【问题描述】:
使用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;
}
}
【问题讨论】:
标签:
ios
orientation
face-detection
【解决方案1】:
为了涵盖所有这些,并且在没有幻数分配的情况下这样做(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;
}
}
【解决方案2】:
在 Swift 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
}
}
【解决方案3】:
斯威夫特 4:
func convertImageOrientation(orientation: UIImageOrientation) -> CGImagePropertyOrientation {
let cgiOrientations : [ CGImagePropertyOrientation ] = [
.up, .down, .left, .right, .upMirrored, .downMirrored, .leftMirrored, .rightMirrored
]
return cgiOrientations[orientation.rawValue]
}