2016-08-08 57 views
0

我正在運行AVCaptureSession,以便在用戶操作(按下按鈕)時從它獲取靜止圖像。我已經與90度旋轉的方向有問題,雖然我設置這些值:AVCaptureStillImageOutput圖像方向不對

- (void)setupAVCapture { 
    //Capture session 
    _session = nil; 
    _session = [[AVCaptureSession alloc] init]; 
    [_session setSessionPreset:AVCaptureSessionPreset1280x720]; 
//preview view 
    self.previewLayer = nil; 
    self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_session]; 
    [self.previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill]; 

    CALayer *rootLayer = [_previewView layer]; 
    [rootLayer setMasksToBounds:YES]; 
    [[_previewLayer connection]setVideoOrientation:AVCaptureVideoOrientationPortrait]; 
} 

而且

- (IBAction)captureImage:(id)sender { 

    AVCaptureConnection *stillImageConnection = [stillImageOutput connectionWithMediaType:AVMediaTypeVideo]; 
    [stillImageConnection setVideoOrientation:AVCaptureVideoOrientationPortrait]; 
    [stillImageOutput captureStillImageAsynchronouslyFromConnection:stillImageConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) { 
     if (imageDataSampleBuffer != nil) { 
      NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer]; 
      CGDataProviderRef dataProvider = CGDataProviderCreateWithCFData((CFDataRef)imageData); 
      CGImageRef cgImageRef = CGImageCreateWithJPEGDataProvider 
      (dataProvider, nil, YES, kCGRenderingIntentDefault); 

      _capturedImage = [UIImage imageWithCGImage:cgImageRef scale:1.0 orientation:UIImageOrientationUp]; 
     } 
    }]; 

由於提前

+0

試試這個,我覺得這是同樣的問題。 http://stackoverflow.com/questions/3561738/why-avcapturesession-output-a-wrong-orientation –

+0

我的應用程序是肖像,我設置會議的方向和靜止圖像方向肖像和輸出圖像方向UIImageOrientationUp!但圖像旋轉到正確的 – OXXY

+0

你有沒有得到這個工作,我似乎有一個類似的問題。 – BarrettJ

回答

0

與UIImageOrientationLeft方向嘗試。

+0

作爲imageWithCGImage的參數? – OXXY