- (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
我做错了什么?为什么它向我显示错误的方向?
谢谢!
答案 0 :(得分:0)
不确定你是否弄清楚这一点,但你没有做错任何事。需要注意的是,AVCaptureVideoOrientation
和UIImageOrientation
实际上指的是不同的方向!
UIImageOrientationUp
实际上是指横向模式下的设备,音量控制指向地面。所以UIImageOrientationLeft
是肖像照片的正确方向,电源按钮指向天空。