このことを理解しようとして髪をたくさん引っ張った後、私はほとんど禿げています。私は avFoundation で写真を撮ろうとしていますが、ほとんどの場合、向きがめちゃくちゃです。デバイスの向きをキャプチャするために、ViewWillAppear に次の関数があります。
- (void)deviceOrientationDidChange {
UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
if (deviceOrientation == UIDeviceOrientationPortrait)
orientation = AVCaptureVideoOrientationPortrait;
else if (deviceOrientation == UIDeviceOrientationPortraitUpsideDown)
orientation = AVCaptureVideoOrientationPortraitUpsideDown;
// AVCapture and UIDevice have opposite meanings for landscape left and right (AVCapture orientation is the same as UIInterfaceOrientation)
else if (deviceOrientation == UIDeviceOrientationLandscapeLeft)
orientation = AVCaptureVideoOrientationLandscapeRight;
else if (deviceOrientation == UIDeviceOrientationLandscapeRight)
orientation = AVCaptureVideoOrientationLandscapeLeft;
// Ignore device orientations for which there is no corresponding still image orientation (e.g. UIDeviceOrientationFaceUp)
}
このコードは正常に動作しているようで、適切な向きをキャプチャします。次に、次のコードを実行して画像を生成します。
[myConnection setVideoOrientation:self.orientation];
//THIS IS THE CORRECT ORIENTATION
NSLog(@"ORIENTATION %d",myConnection.videoOrientation);
[self.stillOutput captureStillImageAsynchronouslyFromConnection:myConnection completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error)
{
if (imageSampleBuffer != nil) {
NSData *jpeg = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer]; //jpeg image in binary format
UIImage *image = [[UIImage alloc] initWithData:jpeg];
NSLog(@"IMAGE ORIENTATION: %d", image.imageOrientation);
postProcessingBlock(image, mutable);
[mutable release];
[image release];
}
else {
NSLog(@"AVCapture ERROR:%@", [error localizedDescription]);
}
}];
背面カメラ (ボタンを押した状態) を使用してポートレート モードで写真を撮ると、ログに次のように表示されます。
ORIENTATION 1
IMAGE ORIENTATION 3
私は何を間違っていますか?間違った向きが表示されるのはなぜですか?
ありがとう!