1

有谁知道我如何将实时摄像机输入到UIImageView?

我有一个自定义 UI,我需要在其中显示摄像头馈送(前置摄像头),因此我无法使用UIImagePickerControl.

4

1 回答 1

4

您需要创建一个捕获会话,并启动它运行。完成后,您可以将捕获会话中的图层添加到您的视图中:

- (void)setupCaptureSession
{
    NSError* error = nil;

    // Create the session
    _captureSession = [[AVCaptureSession alloc] init];    
    _captureSession.sessionPreset = AVCaptureSessionPresetMedium;
    AVCaptureDevice* device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    AVCaptureDeviceInput* input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];

    [_captureSession addInput:input];
    AVCaptureVideoDataOutput* output = [[AVCaptureVideoDataOutput alloc] init];
    [_captureSession addOutput:output];

    // Configure your output.
   dispatch_queue_t queue = dispatch_queue_create("myCameraOutputQueue", NULL);
   //If you want to sebsequently use the data, then implement the delegate.
   [output setSampleBufferDelegate:self queue:queue]; 
}

完成后,您可以创建一个预览层,如下所示:

_previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:_captureSession];
[_captureSession startRunning];

然后将预览层添加到您的视图中:

_myView.layer addSubLayer:_previewLayer];
于 2013-01-15T12:21:26.557 回答