在拉了这么多头发想弄清楚这件事后,我几乎秃顶了。我正在尝试使用 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
我究竟做错了什么?为什么它向我显示错误的方向?
谢谢!