4

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

3 に答える 3

5

それらすべてをカバーし、マジックナンバーを割り当てずにこれを行うには(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;
    }
}
于 2015-03-24T18:46:24.217 に答える
1

Swift4では

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
  }
}
于 2018-05-26T12:41:06.363 に答える
0

スウィフト4:

func convertImageOrientation(orientation: UIImageOrientation) -> CGImagePropertyOrientation  {
    let cgiOrientations : [ CGImagePropertyOrientation ] = [
        .up, .down, .left, .right, .upMirrored, .downMirrored, .leftMirrored, .rightMirrored
    ]

    return cgiOrientations[orientation.rawValue]
}
于 2017-09-18T05:08:49.287 に答える