AVCapturePhotoOutput没有返回正确的图像

时间:2017-12-26 07:02:25

标签: objective-c avfoundation avcapturedevice

我在应用程序中的要求是在不预览- (void)clickImage { AVCaptureDevice *rearCamera = [self checkIfRearCameraAvailable]; if (rearCamera != nil) { photoSession = [[AVCaptureSession alloc] init]; NSError *error; AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:rearCamera error:&error]; if (!error && [photoSession canAddInput:input]) { [photoSession addInput:input]; AVCapturePhotoOutput *output = [[AVCapturePhotoOutput alloc] init]; if ([photoSession canAddOutput:output]) { [photoSession addOutput:output]; AVCaptureConnection *videoConnection = nil; for (AVCaptureConnection *connection in output.connections) { for (AVCaptureInputPort *port in [connection inputPorts]) { if ([[port mediaType] isEqual:AVMediaTypeVideo]) { videoConnection = connection; break; } } if (videoConnection) { break; } } if (videoConnection) { [photoSession startRunning]; [output capturePhotoWithSettings:[AVCapturePhotoSettings photoSettings] delegate:self]; } } } } } - (AVCaptureDevice *)checkIfRearCameraAvailable { AVCaptureDevice *rearCamera; AVCaptureDeviceDiscoverySession *captureDeviceDiscoverySession = [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInWideAngleCamera] mediaType:AVMediaTypeVideo position:AVCaptureDevicePositionBack]; NSArray *allCameras = [captureDeviceDiscoverySession devices]; for (int i = 0; i < allCameras.count; i++) { AVCaptureDevice *camera = [allCameras objectAtIndex:i]; if (camera.position == AVCaptureDevicePositionBack) { rearCamera = camera; } } return rearCamera; } - (void)captureOutput:(AVCapturePhotoOutput *)output didFinishProcessingPhotoSampleBuffer:(CMSampleBufferRef)photoSampleBuffer previewPhotoSampleBuffer:(CMSampleBufferRef)previewPhotoSampleBuffer resolvedSettings:(AVCaptureResolvedPhotoSettings *)resolvedSettings bracketSettings:(AVCaptureBracketedStillImageSettings *)bracketSettings error:(NSError *)error { if (error) { NSLog(@"error : %@", error.localizedDescription); } if (photoSampleBuffer) { NSData *data = [AVCapturePhotoOutput JPEGPhotoDataRepresentationForJPEGSampleBuffer:photoSampleBuffer previewPhotoSampleBuffer:previewPhotoSampleBuffer]; UIImage *image = [UIImage imageWithData:data]; _imgView.image = image; } } 的情况下捕获图像所以我使用以下代码来捕获图像而不显示相同的图像。

{{1}}

我已经使用上面的代码来捕获图像但输出的图像返回看起来像在夜间模式或像负像。

请您查看代码并用此更正我吗?

1 个答案:

答案 0 :(得分:0)

我在Apple开发者网站上找到了以下示例代码:

https://developer.apple.com/library/content/samplecode/AVCam/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010112

以上示例代码有助于获得正确的图像。我已将此答案添加到其他人的帮助中。