使用CIDetector来检测图像中的人脸,你需要根据文档指定图像方向,这意味着它与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;
}
}发布于 2015-03-25 02:46:24
为了涵盖它们,并且不使用幻数赋值( 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;
}
}发布于 2018-05-26 20:41:06
在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
}
}发布于 2017-09-18 13:08:49
Swift 4:
func convertImageOrientation(orientation: UIImageOrientation) -> CGImagePropertyOrientation {
let cgiOrientations : [ CGImagePropertyOrientation ] = [
.up, .down, .left, .right, .upMirrored, .downMirrored, .leftMirrored, .rightMirrored
]
return cgiOrientations[orientation.rawValue]
}https://stackoverflow.com/questions/15079864
复制相似问题