【发布时间】:2012-09-05 02:12:30
【问题描述】:
在拉了这么多头发想弄清楚这件事后,我几乎秃顶了。我正在尝试使用 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
我做错了什么?为什么它显示错误的方向?
谢谢!
【问题讨论】:
-
我从这个 SO 中使用了 an0 的解决方案:stackoverflow.com/questions/5427656/…
-
这不行,它保存在图像上的方向错误,认为图像即使不应该旋转也应该旋转
-
我在这里遇到了同样的问题。
标签: ios uiimage avfoundation