AVCaptureStillImageOutput图像方向错误

时间:2016-08-08 12:56:37

标签: ios objective-c avcapturesession

我正在运行AVCaptureSession以通过用户操作(按下按钮)从中获取静态图像。虽然我设置了这些值,但我的方向旋转90度时出现了问题:

- (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];
        }
    }];

先谢谢

1 个答案:

答案 0 :(得分:0)

尝试使用UIImageOrientationLeft方向。