不能让AVFoundation有正确的方向

时间:2012-09-05 02:12:31

标签: ios uiimage avfoundation

拉着这么多头发试图弄明白这件事后,我几乎秃顶了。我试图用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

我做错了什么?为什么它向我显示错误的方向?

谢谢!

1 个答案:

答案 0 :(得分:0)

不确定你是否弄清楚这一点,但你没有做错任何事。需要注意的是,AVCaptureVideoOrientationUIImageOrientation实际上指的是不同的方向!

UIImageOrientationUp实际上是指横向模式下的设备,音量控制指向地面。所以UIImageOrientationLeft是肖像照片的正确方向,电源按钮指向天空。